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. [solved] How to make cell in the Table View a QCombobox when an user edits ?
Forum Updated to NodeBB v4.3 + New Features

[solved] How to make cell in the Table View a QCombobox when an user edits ?

Scheduled Pinned Locked Moved General and Desktop
qcomboboxqtableview
15 Posts 3 Posters 20.4k Views 3 Watching
  • 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.
  • RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by Ratzz
    #4

    @Sam
    My code looks like this
    Mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QTableView>
    #include <QWidget>
    #include <QApplication>
    #include <QFileSystemModel>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    main.cpp

    #include <QtGui/QApplication>
    #include "mainwindow.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        return a.exec();
    }
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QFileSystemModel *model = new QFileSystemModel;//creating model
         model->setRootPath(QDir::currentPath());
    
        QTableView *view = new QTableView; //creating view
         view->setModel(model);
         view->show();//viewing
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    How to use QStyledItemDelegate on the above code?

    --Alles ist gut.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #5

      @Ratzz

      You need to create a separate class as shown in the WIKI document link , Then create an instance and set it to your tableView.

      e,g

      ComboBoxDelegate *comboDelegate = new ComboBoxDelegate(this);
      m_tableView->setItemDelegateForColumn(col,comboDelegate );
      SGaistS 1 Reply Last reply
      0
      • RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by
        #6

        @Sam
        Thanks for the reply .
        I am newbie to QT i did not understand how to create an instance and set it to your tableView.
        I am now trying with QItemDelegate now.
        Can you just tell which part of the code need to be edited ?

        --Alles ist gut.

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

          @Sam
          I tried with QStandardItemModel . But i want tableview be the combobox when edited.

          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              QStandardItemModel *model = new QStandardItemModel(4,2,this);
              ui->tableView->setModel(model);
               for(int row = 0; row < 4; row++)
                       {
                           for(int col = 0; col < 2; col++)
                           {
                               QModelIndex index= model->index(row,col,QModelIndex());
          
                               // 0 for all data
                               model->setData(index, 0) ;
                           }
                       }
          
               QComboBox *combo = new QComboBox(this);
               combo->setModel(model);
          }
          
          

          --Alles ist gut.

          1 Reply Last reply
          0
          • S Sam

            @Ratzz

            You need to create a separate class as shown in the WIKI document link , Then create an instance and set it to your tableView.

            e,g

            ComboBoxDelegate *comboDelegate = new ComboBoxDelegate(this);
            m_tableView->setItemDelegateForColumn(col,comboDelegate );
            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #8

            Hi

            e,g

            ComboBoxDelegate *comboDelegate = new ComboBoxDelegate(this);
            m_tableView->setItemDelegateForColumn(col,comboDelegate );
            

            @Sam gave you the example there ^^^^

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

              @sam @SGaist
              I have created a separate class ComboBoxDelegate which contains same code as the above wiki document .
              where to create an instance and set it to tableView ? in main.cpp or in mainwindow.cpp? or in comboboxitemdelegate.cpp

              --Alles ist gut.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #10

                The place where you can access the instance of tableView or where you instantiate your tableView. So in this case it should be in the constructor of MainWindow

                view->setItemDelegateForColumn(col,comboDelegate );
                1 Reply Last reply
                1
                • RatzzR Offline
                  RatzzR Offline
                  Ratzz
                  wrote on last edited by
                  #11

                  @sam
                  Thanks it works :)

                  --Alles ist gut.

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

                    @Sam
                    I have 2 column and 4 rows if i set

                    view->setItemDelegateForColumn(1,comboDelegate );
                    

                    Entire column "1 " will be a combobox .

                    If i set

                    view->setItemDelegateForRow(1,comboDelegate );
                    

                    only row 1 corresponding to first column will be combobox (one cell only )
                    What if i want only one row to be a combobox?

                    --Alles ist gut.

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

                      @Sam now its working fine :)

                      Is there any option to make particular cell a combobox ?(not entire row or column )

                      --Alles ist gut.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sam
                        wrote on last edited by
                        #14

                        So you mean to say you want to display the combobox only for one specific cell, In this case you can also try with setIndexWidget() which will display a combobox for a particular modelIndex.

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

                          @Sam
                          Thanks you :)
                          Why can't i set using? can't we do it for tableview? is it only for widgets?

                           tableView->setIndexWidget(index,comboDelegate);
                          

                          'QAbstractItemView::setIndexWidget' : cannot convert parameter 2 from 'ComboBoxDelegate *' to 'QWidget *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

                          This works fine

                           tableView->setIndexWidget(index,new QLineEdit);

                          --Alles ist gut.

                          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