Row Background Color GtkTreeView Widget - c++

I'm attempting to color disabled rows in a gtk tree view widget a light gray color. From what I've read, I'm supposed to set the background-gdk property of the corresponding cellrenderer and bind it to a model column. This sort of works.
Gtk::CellRendererText* textRenderer = manage(new Gtk::CellRendererText());
textRenderer->property_editable() = false;
Gtk::TreeViewColumn *col = manage(new Gtk::TreeViewColumn("Column1", *textRenderer));
col->add_attribute(*textRenderer, "background-gdk", m_treeview_columns.m_back_color);
my_treeview.append_column(*col);
Gtk::TreeModel::Row row;
for (int i = 0; i < NUMBER_OF_ROWS; iLane++){
row = *(treeview_liststore->append());
row[m_workListColumns.m_back_color] = Gdk::Color("#CCCCCC");
}
In the end though, I get only the cells colored properly. BUT I also get an ugly white-space in between the cells. Does anyone know of a way to fix this or a better way to achieve the effect I'm after?

Could you set the background of the row to match the cell background or set the bakground of the tree view all together ? Or maybe the cell with cell-background-gdk ?

Related

QTextDocument and selection painting

I am painting a block of text on a widget with QTextDocument::drawContents. My current goal is to intercept mouse events and emulate text selection. It's pretty clear how to handle the mouse, but displaying the result puzzles me a lot.
Just before we start: I can not use QLabel and let it handle selection on it's own (it has no idea how to draw unusual characters and messes up line height (https://git.macaw.me/blue/squawk/issues/59)), nor I can not use QTextBrowser there - it's just a message bubble, I'm not ready to sacrifice performance there.
There is a very rich framework around QTextDocument, but I can not find any way to make it color the background of some fragment of text that I would consider selected. Found a way to make a frame around a text, found a way to draw under-over-lined text, but it looks like there is simply just no way this framework can draw a background behind text.
I have tried doing this, to see if I can take selected fragment under some QTextFrame and set it's style:
QTextDocument* bodyRenderer = new QTextDocument();
bodyRenderer->setHtml("some text");
bodyRenderer->setTextWidth(50);
painter->setBackgroundMode(Qt::BGMode::OpaqueMode); //this at least makes it color background under all text
QTextFrameFormat format = bodyRenderer->rootFrame()->frameFormat();
format.setBackground(option.palette.brush(QPalette::Active, QPalette::Highlight));
bodyRenderer->rootFrame()->setFrameFormat(format);
bodyRenderer->drawContents(painter);
Nothing of this works too:
QTextBlock b = bodyRenderer->begin();
QTextBlockFormat format = b.blockFormat();
format.setBackground(option.palette.brush(QPalette::Active, QPalette::Highlight));
format.setProperty(QTextFormat::BackgroundBrush, option.palette.brush(QPalette::Active, QPalette::Highlight));
QTextCursor cursor(bodyRenderer);
cursor.setBlockFormat(format);
b = bodyRenderer->begin();
while (b.isValid() > 0) {
QTextLayout* lay = b.layout();
QTextLayout::FormatRange range;
range.format = b.charFormat();
range.start = 0;
range.length = 2;
lay->draw(painter, option.rect.topLeft(), {range});
b = b.next();
}
Is there any way I can make this framework do a simple thing - draw a selection background behind some text? If not - is there a way I can unproject cursor position into coordinate translation, like can I do reverse operation from QAbstractTextDocumentLayout::hitTest just to understand where to draw that selection rectangle myself?
You can use QTextCursor to change the background of the selected text. You only need to select one character at a time to keep the formatting. Here is an example of highlighting in blue (the color of the text is highlighted in white for contrast):
void MainWindow::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.fillRect(contentsRect(), QBrush(QColor("white")));
QTextDocument document;
document.setHtml(QString("Hello <font size='20'>world</font> with Qt!"));
int selectionStart = 3;
int selectionEnd = selectionStart + 10;
QTextCursor cursor(&document);
cursor.setPosition(selectionStart);
while (cursor.position() < selectionEnd) {
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); // select one symbol
QTextCharFormat selectFormat = cursor.charFormat();
selectFormat.setBackground(Qt::blue);
selectFormat.setForeground(Qt::white);
cursor.setCharFormat(selectFormat); // set format for selection
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1);
}
document.drawContents(&painter, contentsRect());
QMainWindow::paintEvent(event);
}

How to stop expansion of QGridLayout element

I have couple of labels (dynamically generated on button click and can vary in numbers which read from configuration file) and added to QGridLayout. Upon load which looks like below ...
these labels are generated and added as below
QGridLayout * layout = new QGridLayout(ui->pageInputWindow);
layout->setSpacing(5);
layout->setMargin(0);
ui->pageInputWindow->setLayout(layout);
// for question purpose I take number of row and col to 3
// but in actual they will be read from configuration file
for(int row = 0; row < 3; row ++)
{
for(int col = 0; col < 3; col++)
{
QLabel * placeHolder = new QLabel(ui->pageInputWindow);
placeHolder->setText("LABEL "+QString::number(10*(row+1) + col + 1));
layout->addWidget(placeHolder, row, col);
}
}
These label are placed here for rendering video streaming by setting pixmap of captured video frames when selected. I am setting pix as ....
// I am getting img object from other class, below code is just a snap of it.
// You can assume img as valid QImage objcet
QImage img
selectedLabel->setPixmap(QPixmap::fromImage(img).scaled(selectedLabel->size(),Qt::KeepAspectRatio, Qt::FastTransformation));
My ideal scenario is when I select any label it will remain with size which it got when loaded according to configuration settings at runtime. But when I start streaming below event happened.
Suddenly selected label ( see black pictured label at 'LABEL 11' position) starts expansding to much even causing application to expand. Idealy I want above event as below ( assume white background is video frame ) without any expansion.
My question is how can I stop this undesirable expansion of labels while setting pix map on them ?
Set the labels' size policies to ignored:
placeHolder->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

How to change the background color from the header(horizontal / vertical) QTableWidget on Qt?

I would like to know how to change the background color from the headers (horizontal / vertical) from the object QTableWidget on Qt.
I already Know how to change all the headers together, using :
ui->tableWidget->setStyleSheet("QHeaderView::section {background-color:red}");
But I need change individually the items. Obviously if this is possible .
There are at least 2 ways to solve this problem. Very easy one:
Just use setHeaderData() and set specific colors for specific sections.
QTableView *tview = new QTableView;
QStandardItemModel *md = new QStandardItemModel(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
md->setItem(row, column, item);
}
}
tview->setModel(md);
tview->model()->setHeaderData(0,Qt::Horizontal,QBrush(QColor("red")),Qt::BackgroundRole);
tview->show();
But u nfortunately it will not work on some systems... Qt uses the platform style. For example, my Windows doesn't allow to change header's color. So this code doesn't work on my machine. Fortunately, it can be solved easily with changing global style. So next code works:
//... same code ...
tview->show();
QApplication::setStyle(QStyleFactory::create("Fusion"));
If you don't want to change style, then you should create your own HeaderView. Probably, something similar as here.

How to set opacity in QtableWidget cell when setting cell background color

I am trying to insert few rows in a QtableWidget and setting the cell background color as follows:
int rowcount = tableWidget->rowCount();
tableWidget->insertRow(rowcount);
QTableWidgetItem *coloItem = new QTableWidgetItem("");
tableWidget->setItem(rowcount, 0, coloItem);
coloItem->setBackground(Qt::GlobalColor(i)); //Qt::GlobalColor(fColor)
Now I want to set opacity of the cell , how can i do it.
I couldnot find anything related to QtableWidget.

How to change UITabBarItem position?

I need to change UITabBarItem's image and text position when it's highlighted. Is it possible?
It's look like there is no way to change UITabBarItem's image's position. I just corrected my selected image to have about 15 pixels in button and corrected UITabBarItem's title's position on 15 in tabBar:didSelectItem: method of my custom UITabBarController:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
// If there is another selected item set default properties to it.
for (int counter = 0; counter < [self.tabBar.items count]; counter++) {
UITabBarItem *currentSelectedItem = [self.tabBar.items objectAtIndex:counter];
[currentSelectedItem setTitlePositionAdjustment:UIOffsetMake(0, 0)];
}
// Set selected item.
UITabBarItem *selectedItem = item;
[selectedItem setTitlePositionAdjustment:UIOffsetMake(0, -15)];
}