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



  • I am using QFileSystemModel to set model and view it using Qtableview.
    When any user edits the cell of the table i want the cell to be editable and make that particular cell as combobox .



  • Hi,

    You can use a QStyledItemDelegate to your tableView. You can also have a look at this WIKI documentation that uses a combobox delegate.



  • This post is deleted!


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



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


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



  • @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);
    }
    
    

  • Lifetime Qt Champion

    Hi

    e,g

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

    @Sam gave you the example there ^^^^



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



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


  • @sam
    Thanks it works :)



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



  • @Sam now its working fine :)

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



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



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

Log in to reply
 

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