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. Displaying TableWidgetItem

Displaying TableWidgetItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 2.3k 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.
  • A Offline
    A Offline
    Armin
    wrote on last edited by Armin
    #1

    Hi,
    I stored the location of item in QTableWidgetItem but when im trying to display the item on another table widget nothing will show.
    What im doing wrong?
    Here is the code:

    void MainWindow::on_pushButton_2_clicked()
    {
        int row1 = 1;
    while(row1 <= rowfirstlastname){
    int clm1= 1;
            while(clm1 <= 7){
        QTableWidgetItem *table3 = table->item(row1 , clm1 );
        table3 = new QTableWidgetItem;    
        ui->tableWidget->setItem(row1,clm1,table3);
     clm1++;
    
            }
            row1++;
    
        }
    
    

    Thanks

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

      Hi,

      You are creating a new item and setting that one on ui->tableWidget.

      By the way, if you want to move the item around you should use takeItem.

      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
      1
      • A Offline
        A Offline
        Armin
        wrote on last edited by
        #3

        Thanks for your replay

        by the way i have used takeitem but didnt worked.

        void MainWindow::on_pushButton_2_clicked()
        {
            int row1 = 1;
        while(row1 <= rowfirstlastname){
        int clm1= 1;
                while(clm1 <= 7){
            QTableWidgetItem *table3 = table->takeItem(row1 , clm1 );
            table3 = new QTableWidgetItem;
            ui->tableWidget->setItem(row1,clm1,table3);
            ui->label_3->setText(table3->text());
            clm1++;
                }
                row1++;
            }
        }
        ``
        

        there is one more question.
        im putting strings on another table with this code :

        QString model = ui->textEdit_2->toPlainText();
            QTableWidgetItem *itemobj3 = new QTableWidgetItem;
            itemobj3->setText(model);
            table->setItem(rowfirstlastname , 3 , itemobj3 );`
        

        is there any wrong with this too?

        Thanks.

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

          Because you are still doing the same wrong thing:

          QTableWidgetItem *table3 = table->takeItem(row1 , clm1 );
              table3 = new QTableWidgetItem; << You are replacing the item you took with a new empty one.
          

          which means you are also leaking memory.

          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
          1
          • A Offline
            A Offline
            Armin
            wrote on last edited by
            #5

            i have removed that line but program will crash.
            Thanks.

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

              When does it crash ?
              What does the backtrace tell you ?
              Are you sure all the constants you use are valid ?

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

              A 1 Reply Last reply
              1
              • SGaistS SGaist

                When does it crash ?
                What does the backtrace tell you ?
                Are you sure all the constants you use are valid ?

                A Offline
                A Offline
                Armin
                wrote on last edited by
                #7

                @SGaist when i click on push button 2 it will crash
                its better to put my whole code :

                .h

                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();
                    int row=1;
                    int clm=7;
                    int rowfirstlastname = 1;
                    QTableWidget *table = new QTableWidget (row,clm);
                
                private slots:
                    void on_pushButton_clicked();
                
                    void on_pushButton_2_clicked();
                
                private:
                    Ui::MainWindow *ui;
                };
                

                .cpp

                void MainWindow::on_pushButton_clicked()
                {
                    QString name = ui->textEdit->toPlainText();
                    QTableWidgetItem *itemobj1 = new QTableWidgetItem;
                    itemobj1->setText(name);
                    ui->tableWidget->setItem(rowfirstlastname , 1 , itemobj1 );
                
                    QString make = ui->comboBox_2->currentText();
                    QTableWidgetItem *itemobj2 = new QTableWidgetItem;
                    itemobj2->setText(make);
                    ui->tableWidget->setItem(rowfirstlastname , 2 , itemobj2 );
                
                    QString model = ui->textEdit_2->toPlainText();
                    QTableWidgetItem *itemobj3 = new QTableWidgetItem;
                    itemobj3->setText(model);
                    ui->tableWidget->setItem(rowfirstlastname , 3 , itemobj3 );
                
                    QString year = ui->comboBox_3->currentText();
                    QTableWidgetItem *itemobj4 = new QTableWidgetItem;
                    itemobj4->setText(year);
                    ui->tableWidget->setItem(rowfirstlastname , 4 , itemobj4 );
                
                    QString type = ui->comboBox->currentText();
                    QTableWidgetItem *itemobj5 = new QTableWidgetItem;
                    itemobj5->setText(type);
                    ui->tableWidget->setItem(rowfirstlastname , 5 , itemobj5 );
                
                    QString color = ui->comboBox_3->currentText();
                    QTableWidgetItem *itemobj6 = new QTableWidgetItem;
                    itemobj6->setText(color);
                    ui->tableWidget->setItem(rowfirstlastname , 6 , itemobj6 );
                
                    QString tag = ui->textEdit_3->toPlainText();
                    QTableWidgetItem *itemobj7 = new QTableWidgetItem;
                    itemobj7->setText(tag);
                    ui->tableWidget->setItem(rowfirstlastname , 7 , itemobj7 );
                
                    rowfirstlastname++;
                
                }
                
                void MainWindow::on_pushButton_2_clicked()
                {
                    int row1 = 1;
                while(row1 <= rowfirstlastname){
                int clm1= 1;
                        while(clm1 <= 7){
                    QTableWidgetItem *table3 = ui->tableWidget->takeItem(row1 , clm1 );
                    ui->tableWidget->setItem(row1,clm1,table3);
                    table3 = new QTableWidgetItem;
                    ui->label_3->setText(table3->text());
                    clm1++;
                        }
                        row1++;
                    }
                }
                
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  From what I gather you have seven columns in your table. In C++, everything that has an index is zero based i.e. a seven element table is accessed from 0 to 6. Therefore your while(clm1 <= 7) loop is wrong.

                  Second thing:

                  QTableWidgetItem *table3 = ui->tableWidget->takeItem(row1 , clm1 );
                      ui->tableWidget->setItem(row1,clm1,table3); << Why are you taking the item out of tableWidget to put it back at the exact same place ?
                  

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

                  A 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    From what I gather you have seven columns in your table. In C++, everything that has an index is zero based i.e. a seven element table is accessed from 0 to 6. Therefore your while(clm1 <= 7) loop is wrong.

                    Second thing:

                    QTableWidgetItem *table3 = ui->tableWidget->takeItem(row1 , clm1 );
                        ui->tableWidget->setItem(row1,clm1,table3); << Why are you taking the item out of tableWidget to put it back at the exact same place ?
                    
                    A Offline
                    A Offline
                    Armin
                    wrote on last edited by
                    #9

                    @SGaist So how i must display the item that i stored in table
                    thanks

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

                      Use the correct indexes and also in your while loop you overwrite the content of label_3 every time so at best you'll see the content of the last column of the last row.

                      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