PDA

View Full Version : QTableWidget separtion lines disappearing



tuli
26th December 2018, 01:31
Hi,

I have a perhaps somewhat unusual setup of a QTableWidget, where the tablewidget contains QLabels as cell-widgets, and those labels contain pixmaps to render something.
The problem: when I drag the columnsseparator to the left, the column separation line disappears.

I have created a simple example of the problem below. Just run it and play around with resizing the columns by dragging the header. Quickly the separation line will disappear. Screenshot:

13001

code:



class mm : public QWidget
{
Q_OBJECT
public:
mm(QWidget* parent = nullptr) : QWidget(parent)
{
setFixedSize(500,500);
table.setColumnCount(2);
table.setRowCount(1);
table.verticalHeader()->setStretchLastSection(true);

table.setCellWidget(0, 0, l1 = new QLabel());
table.setCellWidget(0, 1, l2 = new QLabel());

auto l = new QHBoxLayout();
l->addWidget(&table);
setLayout(l);
}

QLabel *l1, *l2;
QTableWidget table;

void paintEvent(QPaintEvent* e) override
{
{
QPixmap pm(l1->size());
pm.fill(Qt::red);
l1->setPixmap(pm);
}
{
QPixmap pm(l2->size());
pm.fill(Qt::black);
l2->setPixmap(pm);
}
}
};


Why does this happen and how can I ensure the lines are always drawn?

Happy Holidays!

anda_skoa
26th December 2018, 11:58
Not necessarily the cause but it is usually not a good idea to override the paintEvent() method of a container.
Especially when not calling the base implementation.

If your cells really only contain a single color, my suggestion would be to set the background of the respective QTableWidgetItem instead.
http://doc.qt.io/qt-5/qtablewidgetitem.html#setBackground

Cheers,
_

tuli
26th December 2018, 12:50
Unfortuantley the single colors were just for demonstration.

Thanks for the tip, but the paint-event-overriding is indeed no the cause here. Indeed the issue even occurs when I set the pixmaps only once.



class mm : public QWidget
{

//..as above, but without overriding paintEvent()

void init()
{
{
QPixmap pm(l1->size());
pm.fill(Qt::red);
l1->setPixmap(pm);
}
{
QPixmap pm(l2->size());
pm.fill(Qt::black);
l2->setPixmap(pm);
}
}
};

...
mm m;
m.show();
m.init();
...



Do you think this might be a Qt bug?

anda_skoa
26th December 2018, 15:57
Even if the pixmap is more complex, it make still sense to avoid cell widgets.

Set the pixmal as each item's DecorationRole or go for a custom delegate.

Cheers,
_