Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCombobox in a QTreeView
Forum Updated to NodeBB v4.3 + New Features

QCombobox in a QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wurgl
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #2

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

      --Alles ist gut.

      W 1 Reply Last reply
      0
      • RatzzR Ratzz

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

        W Offline
        W Offline
        Wurgl
        wrote on last edited by
        #3

        @Ratzz Sorry, no change in behaviour :-(

        1 Reply Last reply
        0
        • RatzzR Offline
          RatzzR Offline
          Ratzz
          wrote on last edited by
          #4

          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));

          --Alles ist gut.

          W 2 Replies Last reply
          1
          • RatzzR Ratzz

            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));
            W Offline
            W Offline
            Wurgl
            wrote on last edited by Wurgl
            #5

            @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?

            1 Reply Last reply
            0
            • RatzzR Ratzz

              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));
              W Offline
              W Offline
              Wurgl
              wrote on last edited by
              #6

              @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?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved