简体   繁体   English

QStyledItemDelegate - 创建编辑器后立即显示按钮菜单

[英]QStyledItemDelegate - show button menu as soon as the editor is created

I have a QTableView which shows a "Color" column, upon which the user clicks, a button with menu, which has a color picker widget should be created.我有一个 QTableView,它显示一个“颜色”列,用户单击该列,应创建一个带有菜单的按钮,该按钮具有一个颜色选择器小部件。 I don't have issue with creating and displaying this button on the table cell.我在表格单元格上创建和显示此按钮没有问题。 But my concern is that to get to the color picker widget and select the proper color, user has to do 3 clicks.但我担心的是,要进入颜色选择器小部件并选择正确的颜色,用户必须点击 3 次。

  1. First click - Cell becomes editable and the button appears第一次单击 - 单元格变为可编辑并出现按钮
  2. Second click - Button displays the menu with color picker widget.第二次单击 - 按钮显示带有颜色选择器小部件的菜单。
  3. Third click - User selects the desired color from color picker widget第三次单击 - 用户从颜色选择器小部件中选择所需的颜色

I want to reduce this to two clicks, so that user should be able to achieve all the above.我想将其减少到两次点击,以便用户应该能够实现上述所有目标。 That is,那是,

  1. First click - Cell becomes editable and the button appears, at the same time menu with color picker also shows up第一次单击 - 单元格变为可编辑并出现按钮,同时也显示带有颜色选择器的菜单
  2. Second click - User selects the desired color from picker第二次单击 - 用户从选择器中选择所需的颜色

What I was trying to do calling colorButton->showMenu() in different places of QStyledItemDelegate overriden functions, for example in setEditorData() like this.我试图在 QStyledItemDelegate 覆盖函数的不同位置调用 colorButton->showMenu() ,例如在像这样的 setEditorData() 中。

void qDrawToolsDelegate_C::setEditorData(QWidget * editor, const QModelIndex & index) const
{
    if (!index.isValid() || !editor) return;
    const qDrawToolsModel_C * model = static_cast<const qDrawToolsModel_C *>(index.model());
    if (!model) return;
    if (model->columnType(index.column()) == COL_TYPE_COLOR) {
        qColorButton_C * colorButton = static_cast<qColorButton_C *>(editor);
        colorButton->setColor(_model->getColor(index));
        colorButton->showMenu();
    }
    else QStyledItemDelegate::setEditorData(editor, index);
}

The menu shows up, but the button doesn't seem to appear properly.菜单显示出来,但按钮似乎没有正确显示。 I assume that the menu pops up before the painting on button is complete.我假设菜单在按钮上的绘画完成之前弹出。 See picture 1 (click on Red cell).见图1(点击红色单元格)。

单击单元格时弹出菜单,但按钮不可见

When the popup is closed, the button draws properly.当弹出窗口关闭时,按钮会正确绘制。 See picture 2.见图2。

弹出菜单关闭后按钮绘制正确

I'm not sure how showing popup blocks the painting on the button.我不确定显示弹出窗口如何阻止按钮上的绘画。 If it is really because of that, I can think of creating a timer and then call showMenu() in timer slot to solve this.如果真的是因为这个,我可以考虑创建一个计时器,然后在计时器槽中调用 showMenu() 来解决这个问题。 But I like you guys' suggestions on how to achieve this better.但我喜欢你们关于如何更好地实现这一目标的建议。

PS: I just RTFM :). PS:我只是 RTFM :)。 It says about showMenu() -"This function does not return until the popup menu has been closed by the user."它说关于 showMenu() -“在用户关闭弹出菜单之前,此函数不会返回。”

I was under the impression that when QStyledItemDelegate::setEditorData() is called it is already fully displayed, which I think is not the case.我的印象是,当 QStyledItemDelegate::setEditorData() 被调用时,它已经完全显示了,我认为事实并非如此。 The solution was to add a singleshot timer in setEditorData() to invoke showMenu().解决方案是在 setEditorData() 中添加一个单发计时器来调用 showMenu()。

void qDrawToolsDelegate_C::setEditorData(QWidget * editor, const QModelIndex & index) const
{
    if (!index.isValid() || !editor) return;
    const qDrawToolsModel_C * model = static_cast<const qDrawToolsModel_C *>(index.model());
    if (!model) return;
    if (model->columnType(index.column()) == COL_TYPE_COLOR) {
        qColorButton_C * colorButton = static_cast<qColorButton_C *>(editor);
        colorButton->setColor(_model->getColor(index));
        QTimer::singleShot(5, colorButton, SLOT(showMenu()));

    }
    else QStyledItemDelegate::setEditorData(editor, index);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM