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



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


  • Lifetime Qt Champion

    Hi,

    Are these data coming from a database ?



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


  • @SGaist yes of course



  • Then even easier to use 1 QSqlQueryModel or QSqlTableModel



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