[SOLVED] Additional widget like in QComboBox

  • How to create a widget for QPushButton, that show widget like QComboBox?
    I want to create a widget like QComboBox, but in this additional widget I need to see QTableWidget, not a QListWidget (or what is there).

  • Moderators

  • I tried it. But code like this has no effect...
    @void showPopup()
    QStandardItemModel *model = new QStandardItemModel(3,3);
    Where is my problem?

  • Your view is still a QListView, not a QTableView. Lucky for you, QComboBox also has a setView method...

  • Amazing! Thanks a lot. Now it work as I want.

  • I have new problem.
    I didn't understand how to reimplement addItem() function to add items to model/table.
    If I added new item to QStandardItemModel, which I set to view() in constructor, then my combobox are disabled. I can't open it.

    @MyComboBox::MyComboBox(QWidget *parent) : QComboBox(parent)
    setView(new QTableView);
    model = new QStandardItemModel(3,3);

    void MyComboBox::addItem(int row, int col, const QString &text)
    QStandardItem *item = new QStandardItem(text);
    model->setItem(row,col,item); // model - is a global QStandardItemModel

    Where is my problem? Should I add items directly to default QAbstractItemModel?

  • First of all: don't use globals unless you really, really have to (basically: never). What happens in your case if you want more than one of these special combo boxes in your application? Just make the model a member of the class.

    I am not sure how nice QCombobox plays if you use your own model and not the one that QComboBox is aware of, but my guess would be: not nice. If the QComboBox's model is empty, then it makes sense QComboBox hides the drop down, right? So, why don't you tell QComboBox about your own model by calling QComboBox::setModel()?

  • Thanks.
    QComboBox::setModel() helps.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.