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. Changing a QTableWidget rows based on the changes made to another QTableWidget rows

Changing a QTableWidget rows based on the changes made to another QTableWidget rows

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatorqtablewidgetindexchange
5 Posts 3 Posters 2.8k 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.
  • K Offline
    K Offline
    Kushan
    wrote on 28 Dec 2017, 16:32 last edited by A Former User 1 Mar 2018, 18:07
    #1

    In my Qt c++ application I have 2 single column table widgets! Each table widget has 10 rows and one table widget has userIDs from 0 to 9 and the other table widget has corresponding user names for each userID.

    following is my code

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        QStringList Headers1;
        QStringList Headers2;
    
        QStringList t1List1;
    
        QStringList t2List1;
    
    
        QStringList t1List1amend;
    
    
        QStringList t2List1amend;
        QStringList t2List2amend;
    
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_3_clicked();
    
        void on_pushButton_4_clicked();
        void getChangedIndices();
    
    
    
    
        void on_pushButton_5_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->tableWidget->setRowCount(10);
        ui->tableWidget->setColumnCount(1);
    
        ui->tableWidget_2->setRowCount(10);
        ui->tableWidget_2->setColumnCount(1);
    
        ui->tableWidget->setColumnWidth(0,150);
        ui->tableWidget_2->setColumnWidth(0,150);
    
    
        Headers1<<"UserID";
        Headers2<<"UserName";
        ui->tableWidget->setHorizontalHeaderLabels(Headers1);
        ui->tableWidget_2->setHorizontalHeaderLabels(Headers2);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_3_clicked()
    {
        t1List1<<"0"<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9";
        t2List1<<"John"<<"Smith"<<"Anne"<<"Kamal"<<"Yohan"<<"Aruna"<<"Yusuf"<<"ABC"<<"Shamal"<<"Lasith";
    
        for(int i=0;i<t1List1.size();i++){
            ui->tableWidget->setItem(i,0,new QTableWidgetItem(t1List1[i]));
        }
    
    
        for(int i=0;i<t2List1.size();i++){
            ui->tableWidget_2->setItem(i,0,new QTableWidgetItem(t2List1[i]));
        }
    
    }
    
    void MainWindow::on_pushButton_4_clicked()
    {
        for(int i=0;i<t1List1.size();i++){
            t1List1amend<<ui->tableWidget->item(i,0)->text();
        }
    
    
    }
    
    
    void MainWindow::getChangedIndices(){
        for(int i=0;i<t1List1amend.size();i++){
            if(t1List1amend[i]!=t1List1[i]){
    
    
                t2List1.replace(i,t2List1[t1List1amend[i].toInt()]);
                t2List1amend<<t2List1[i];
    
            }
            else{
                t2List1amend<<t2List1[i];
            }
        }
    
    }
    void MainWindow::on_pushButton_5_clicked()
    {
       getChangedIndices();
    }
    

    when push button 3 is clicked the two table widgets are displayed with userIDs and USernames. After changing the user IDs( eg- USerID 1 replaced with 4 and userID 4 replaced with one) push button 4 is clicked so that a QStringList with changed userIDs are created.

    the getChangedIDs is used to rearrange the userNames so that the change of userIDs are depicted.(eg- previously I changed user ID 1 to 4 and userID 4 to 1).

    so after clicking push button 5 userName corresponding to userID 1("Smith") should be replaced by "Yohan" and Yohan should be replaced by "Smith". currently Smith is replaced by Yohan but Yohan does not change!( 2 Yohan's are there in the final tablewidget without any smith)

    How can I correct my code?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Dec 2017, 09:00 last edited by
      #2

      Hi,

      Are these data coming from a database ?

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

      K 1 Reply Last reply 29 Dec 2017, 10:13
      0
      • V Offline
        V Offline
        VRonin
        wrote on 29 Dec 2017, 09:30 last edited by VRonin
        #3

        You are way over complicating this.

        Use a single model and 2 QTableViews instead of 2 QTableWidgets.
        in MainWindow.h, after private: add QAbstractItemModel* model;

        in MainWindow.cpp

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        model = new QStandardItemModel(this);
        const QString userNames[] = {QStringLiteral("John"),QStringLiteral("Smith"),QStringLiteral("Anne"),QStringLiteral("Kamal"),QStringLiteral("Yohan"),QStringLiteral("Aruna"),QStringLiteral("Yusuf"),QStringLiteral("ABC"),QStringLiteral("Shamal"),QStringLiteral("Lasith")} ;
        const int usernameSize = std::extent<decltype(userNames)>::value; // this should be 10
        model->insertColumns(0,2);
        model->insertRows(0,usernameSize );
        model->setHeaderData(0,Qt::Horizontal,QStringLiteral("UserID"));
        model->setHeaderData(1,Qt::Horizontal,QStringLiteral("UserName"));
        for(int i=0;i<usernameSize ;++i){
        model->setData(model->index(i,0),i);
        model->setData(model->index(i,1),userNames[i]);
        }
        ui->tableView->setModel(model);
        ui->tableView->setColumnHidden(1,true);
        ui->tableView_2->setModel(model);
        ui->tableView_2->setColumnHidden(0,true);
        }
        void MainWindow::on_pushButton_5_clicked()
        {
           model->sort(0);
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • S SGaist
          29 Dec 2017, 09:00

          Hi,

          Are these data coming from a database ?

          K Offline
          K Offline
          Kushan
          wrote on 29 Dec 2017, 10:13 last edited by
          #4

          @SGaist yes of course

          V 1 Reply Last reply 29 Dec 2017, 10:35
          0
          • K Kushan
            29 Dec 2017, 10:13

            @SGaist yes of course

            V Offline
            V Offline
            VRonin
            wrote on 29 Dec 2017, 10:35 last edited by VRonin
            #5

            Then even easier to use 1 QSqlQueryModel or QSqlTableModel

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            1/5

            28 Dec 2017, 16:32

            • Login

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