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. Adding a column to tableview
QtWS25 Last Chance

Adding a column to tableview

Scheduled Pinned Locked Moved Unsolved General and Desktop
tableviewcolumn
10 Posts 3 Posters 13.4k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • ValentinMicheletV Offline
      ValentinMicheletV Offline
      ValentinMichelet
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        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.
        
        1 Reply Last reply
        0
        • ValentinMicheletV Offline
          ValentinMicheletV Offline
          ValentinMichelet
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 ?

              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
              0
              • G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  A radio button ? What for ?

                  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
                  0
                  • G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    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.
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      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
                      0

                      • Login

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