QCombobox in a QTreeView



  • Hello!

    I found this example and it works fine (just replace those triple quotes by a star).
    https://wiki.qt.io/Combo_Boxes_in_Item_Views/de

    Then modified it a little bit for my problem, I use a QTreeView

    // —— File main.cpp ——
    #include <QApplication>
    #include <QTreeView>
    #include <QStandardItemModel>
     
    #include "itemdelegate.h"
     
    int main(int argc, char *argv[])
    {
     QApplication a(argc, argv);
     QTreeView tw;
     
    ComboBoxItemDelegate *cbid = new ComboBoxItemDelegate(&tw);
     tw.setItemDelegate(cbid);
     QStandardItemModel *m = new QStandardItemModel(&tw);
     QStandardItem *i1 = new QStandardItem();
     m->appendRow(i1);
     QStandardItem *i2 = new QStandardItem();
     m->appendRow(i2);
     tw.setModel(m);
    
     tw.openPersistentEditor(m->indexFromItem(i1));
     tw.openPersistentEditor(m->indexFromItem(i2));
    
     tw.show();
     
    return a.exec();
    }
    

    And of course 'if(index.column() != 0)' in ComboBoxItemDelegate::createEditor. When I start the program, I see the following:
    http://abload.de/img/tableviewflsdw.png

    As you can see, the size of the combobox does not match the size of the items, when resizing the window, the sizes fit sometimes but not always.

    Okay. So it seems I need to implement sizeHint in the delegate class. And this is the point where I have to ask my question: How to determine a resonable size? Any example?



  • I think you should set delegate after setting model http://doc.qt.io/qt-4.8/qabstractitemview.html#setItemDelegate



  • @Ratzz Sorry, no change in behaviour :-(



  • I just tried with this just worked http://postimg.org/image/jtrprz7jx/

    
        ComboBoxItemDelegate *cbid = new ComboBoxItemDelegate();
        QStandardItemModel *m = new QStandardItemModel(10,4,this);
        QStandardItem *i1 = new QStandardItem();
        m->appendRow(i1);
        QStandardItem *i2 = new QStandardItem();
        m->appendRow(i2);
        ui->treeView->setModel(m);
        ui->treeView->setItemDelegate(cbid);
        
        ui->treeView->openPersistentEditor(m->indexFromItem(i1));
        ui->treeView->openPersistentEditor(m->indexFromItem(i2));


  • @Ratzz Strange!

    // —— File main.cpp ——
    #include <QApplication>
    #include <QTreeView>
    #include <QStandardItemModel>
    
    #include "itemdelegate.h"
    
    int main(int argc, char *argv[])
    {
     QApplication a(argc, argv);
     QTreeView tw;
    
    ComboBoxItemDelegate *cbid = new ComboBoxItemDelegate(&tw);
     QStandardItemModel *m = new QStandardItemModel(&tw);
     QStandardItem *i1 = new QStandardItem();
     m->appendRow(i1);
     QStandardItem *i2 = new QStandardItem();
     m->appendRow(i2);
     tw.setModel(m);
     tw.setItemDelegate(cbid);
    
     tw.openPersistentEditor(m->indexFromItem(i1));
     tw.openPersistentEditor(m->indexFromItem(i2));
    
     tw.show();
    
    return a.exec();
    }
    

    That what I use!

    And I tried with Qt 4.8.6 and 5.4.2 (I prefer 5.x, but have 4.x still installed -- no, I do not mix them) on Suse Linux 13.2 / KDE. Both show similar behaviour, just minor changes in the graphics. Did you select one item, then the other, then resize, select again … just fiddling around?



  • @Ratzz You use a different font, maybe this is the reason? I use Sans Serif, pointSize 9. That font seems to come from the KDE settings. Which one do you use?


Log in to reply
 

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