[Interest] More on table problem
Volker Hilsheimer
volker.hilsheimer at qt.io
Sun Jun 13 17:33:23 CEST 2021
>
>>> On Fri, Jun 11, 2021 at 10:05 AM Volker Hilsheimer <volker.hilsheimer at qt.io> wrote:
>>> > On 11 Jun 2021, at 13:52, Turtle Creek Software <support at turtlesoft.com> wrote:
>>> >
>>> > Here's more info on the weird QTableWidget problem we're seeing.
>>> >
>>> > One of our data entry fields uses combination of widgets: a QLineEdit subclass with a linked QToolButton subclass next to it, and a QListWidget subclass that drops down underneath. It acts kinda like a combo box, but better for really long lists. The whole assembly works properly in regular data entry screens. Clicking on button or list changes values in the line edit field.
>>> >
>>> > We use setCellWidget to put the same QLineEdit subclass into a QTableWidget cell. The button and list appear properly, but the button does not catch mouse clicks. They go to the cell behind the button instead.
>>> >
>>> > We use signals/slots to connect the clicked signal, but don't fiddle with events otherwise.
>>> >
>>> > Casey McD
>>>
>>>
>>> This works fine:
>>>
>>> #include <QtWidgets>
>>>
>>> class CellWidget : public QWidget
>>> {
>>> public:
>>> CellWidget()
>>> {
>>> QLineEdit *le = new QLineEdit;
>>> QToolButton *tb = new QToolButton;
>>> QMenu *toolMenu = new QMenu;
>>> toolMenu->addAction(new QAction("Action 1", this));
>>> toolMenu->addAction(new QAction("Action 2", this));
>>> toolMenu->addAction(new QAction("Action 3", this));
>>> tb->setPopupMode(QToolButton::InstantPopup);
>>> tb->setMenu(toolMenu);
>>>
>>> QHBoxLayout *hbox = new QHBoxLayout;
>>> hbox->addWidget(le);
>>> hbox->addWidget(tb);
>>> setContentsMargins(0, 0, 0, 0);
>>> hbox->setContentsMargins(0, 0, 0, 0);
>>>
>>> setLayout(hbox);
>>> }
>>>
>>> QSize minimumSizeHint() const { return QSize(100, 100); }
>>> };
>>>
>>> int main(int argc, char **argv)
>>> {
>>> QApplication app(argc, argv);
>>>
>>> QTableWidget table(10, 10);
>>> table.setCellWidget(5, 5, new CellWidget);
>>>
>>> table.show();
>>>
>>> return app.exec();
>>> }
>>>
>>>
>>> Volker
>>
>> On 11 Jun 2021, at 17:51, Turtle Creek Software <support at turtlesoft.com> wrote:
>>
>> If I understand this code correctly, it's putting the button inside the cell. Unfortunately, there isn't enough room to do that.
>> The table only shows the QLineEdits. When you click in a cell to edit, the button appears next to it, over the neighboring cell(s).
>> However, maybe there is a container we can put the button into, that will act as a click barrier. Something less extreme than a dialog.
>> Casey McD
Make sure you create whatever widget you create for editing cells either via an item delegate, e.g.
https://doc.qt.io/qt-5/qabstractitemdelegate.html#createEditor
But that’s not the same as using setCellWidget, and indeed, QTableView will want to fit the editor widget into the cell, and while you can set a minimum width to make the widget reach beyond the cell, that will not work great on the edge of the table.
If you do something completely custom, then make sure you create child widgets as children of the viewport() of QTableWidget, even if they are popups.
> On 11 Jun 2021, at 18:18, Turtle Creek Software <support at turtlesoft.com> wrote:
>
> I tried putting the button inside a QFrame, but it has the same problem. Casey McD
Feel free to modify my example to show how and when you are setting up those widgets. Otherwise it’s unlikely that we’ll ever end up with the same configuration.
Volker
More information about the Interest
mailing list