Adding a column to tableview



  • Hi,
    What is the best way to add a column to tableview?
    Thank you.



  • Hi,

    I assume that your QTableView has a model. The view only displays available data from its model. You can hide columns that you don't want to be displayed, but you cannot display columns if they are not part of your model.

    So to add a column you have to do it through your model. It's not the best way, it's the only way. It's how the Qt model/view architecture works.
    For further information, see http://doc.qt.io/qt-5/model-view-programming.html.

    What do you try to achieve here exactly?



  • Hi,
    Here is the code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QSqlDatabase db;
               db = QSqlDatabase::addDatabase ("QSQLITE");
               db.setDatabaseName ("C:/Programming/Qtsamples/Image_from_db_to_Table/db.db");
    
               if(!db.open ())
               {
                   qDebug() << "The database is NOT open!";
               }
               else
               {
                   qDebug() << "The database is open!";
               }
    
               QSqlQuery query("SELECT * FROM Items ");
    
    
               if(query.isActive()==true)
                 {
                     qDebug() << "The query is active.";
                 }
                 else
                 {
                    qDebug() << "The query is NOT active.";
                 }
    
               QSqlQuery query2 ("SELECT Count(*) FROM Items");
    
     			int count;
               query2.first ();
                count = query2.value (0).toInt ();
    
    			qDebug() << "The number of rows: " << count;
    
    			//query.first ();
                QStandardItemModel *smodel = new QStandardItemModel;
    
                int ID;
    
    			ui->tableView->setModel (smodel);
    
    //	while(query.next ())
    //    {
    
                      	int row = 0;
    
                        while(query.next ())
                        {
    
                		QStandardItem *Item = new QStandardItem();
                        QStandardItem *Item2 = new QStandardItem();
    
                        Item->setData (ID = query.value (0).toInt (),Qt::DisplayRole);
                        qDebug() << "ID = " <<   ID;
                        smodel->setItem (row,0,Item);
    
                        QByteArray ByteArray;
                        ByteArray = query.value (1).toByteArray ();
                        QPixmap Pixmap;
                        Pixmap.loadFromData (ByteArray);
                        Pixmap = Pixmap.scaled (100,100,Qt::KeepAspectRatio);
    
                        Item2->setData (QVariant(Pixmap),Qt::DecorationRole);
    
                        smodel->setItem (row++,1,Item2);
    			}
    
    
    
    
      					ui->tableView->resizeColumnsToContents ();
       					ui->tableView->resizeRowsToContents ();
              			ui->tableView->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;}");
                        ui->tableView->verticalHeader ()->setVisible (false);
                        ui->tableView->setAlternatingRowColors (true);
                        ui->tableView->setStyleSheet ("alternate-background-color: cyan; background-color: white;");
                        ui->tableView->setEditTriggers (QAbstractItemView::NoEditTriggers);
    
    
              smodel->setHeaderData (0,Qt::Horizontal, QObject::tr ("ID"));
              smodel->setHeaderData (1,Qt::Horizontal, QObject::tr ("Picture"));
    
    
       		db.close ();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    I want to add an empty column as the first column and after that I want to add a radio button to each record.
    Thank you.
    


  • Frist, if you want to display data from a database, you should have a look to these two classes:
    http://doc.qt.io/qt-5/qsqltablemodel.html
    http://doc.qt.io/qt-5/qsqlrelationaltablemodel.html

    That being said, to achieve what you want, you have to subclass the model you picked (either QStandardItemModel or a SQL model, or any Qt model you use). Among others, you will have to reimplement methods that add/insert items in the model, and add your additional data (empty and radio button). Then your QTableView will automatically display these two new columns.

    Here is the documentation to implement your own model :http://doc.qt.io/qt-5/model-view-programming.html#creating-new-models.

    Don't hesitate to ask if you don't understand how to manipulate models.



  • Hi,
    Thank you for your help.
    Would you please help me with adding a blank column to the model. I understand how generally things work but have a really hard time implementing things.
    Thank you.


  • Lifetime Qt Champion

    Hi,

    In addition to what @ValentinMichelet wrote, depending on what you need in your column, writing a proxy model might also be a solution.

    So what will you have in these additional columns ?



  • Hi,
    Planning to add a radio button for each record.


  • Lifetime Qt Champion

    A radio button ? What for ?



  • I want to have 2 options:

    1. Display the record in a form ( not in the table)
    2. Open it for edit in a form.
      I figured it is easier to add a radio button to do it.

  • Lifetime Qt Champion

    You would at least need two radio buttons for that. But it sounds a bit unusual for a UI especially for a database.

    Sounds like you want to use both a custom widget using QDataWidgetMapper and a QTableView at the same time.


Log in to reply
 

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