[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).
    Thanks.


  • Moderators



  • I tried it. But code like this has no effect...
    @void showPopup()
    {
    QComboBox::showPopup();
    QStandardItemModel *model = new QStandardItemModel(3,3);
    view()->setModel(model);
    }@
    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.

    Example:
    @MyComboBox::MyComboBox(QWidget *parent) : QComboBox(parent)
    {
    setView(new QTableView);
    model = new QStandardItemModel(3,3);
    view()->setModel(model);
    addItem(0,0,"A");
    addItem(1,0,"B");
    addItem(2,0,"C");
    }

    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?
    Thanks.



  • 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.