0

I am stuck on a certain problem with table widgets and I am in need of help. Basically, I am trying to get a table widget on a dialogue screen to get the information inside of it and then fill that information on to another table widget on a main window screen. So, when I click the OK button, it should take the text from one table widget and place it in the other table widget. I tried using the below code, and the build ran. But, as soon as I clicked OK, the program crashed. The table called TableWidgetedit is the table I am trying to copy from and send to the table in the mainwindow, named tablewidget. (just to make so I am not vague, I am trying to copy data from the one table and place it in another table, when a user clicks on the OK button.)

    int rows = 6;
    int columns = 5;
    Ui::MainWindow *mainui;
    void EditMode1::on_pushButton_clicked()
    {
       for (int i = 0; i<columns;++i){
           for (int j = 0;j<rows;++j){
             QTableWidgetItem *celltxt= TableWidgetedit.item(j,i);
             mainui->tableWidget->setItem(j,i,celltxt);
           }
        } 
    }

Any and all help is appreciated. Thank you! (I am a not the best with Qt so if you don't mind explaining what changes you have made and why, that would a great help thank you!).

-UPDATE- @Jeet here are the code for what im trying to do:

tablemainwindow1.h:

#ifndef TABLEMAINWINDOW1_H
#define TABLEMAINWINDOW1_H

#include <QMainWindow>
#include "tabledialougewindow.h"

namespace Ui {
class TableMainWindow1;
}

class TableMainWindow1 : public QMainWindow
{
    Q_OBJECT

public:
    explicit TableMainWindow1(QWidget *parent = 0);
    ~TableMainWindow1();

private slots:
    void on_pushButton_clicked();

private:
    Ui::TableMainWindow1 *ui;
    TableDialougeWindow *tbl2;
};

#endif // TABLEMAINWINDOW1_H

tablemainwindow1.cpp:

#include "tablemainwindow1.h"
#include "ui_tablemainwindow1.h"

TableMainWindow1::TableMainWindow1(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::TableMainWindow1)
{
    ui->setupUi(this);
    ui->tableWidget->setRowCount(3);
    ui->tableWidget->setColumnCount(3);
}

TableMainWindow1::~TableMainWindow1()
{
    delete ui;
}


void TableMainWindow1::on_pushButton_clicked()
{
    tbl2 = new TableDialougeWindow(this);
    tbl2->show();
}

tabledialougewindow.h:

#ifndef TABLEDIALOUGEWINDOW_H
#define TABLEDIALOUGEWINDOW_H

#include <QDialog>

namespace Ui {
class TableDialougeWindow;
}

class TableDialougeWindow : public QDialog
{
    Q_OBJECT

public:
    explicit TableDialougeWindow(QWidget *parent = 0);
    ~TableDialougeWindow();

private slots:
    void on_buttonBox_accepted();

private:
    Ui::TableDialougeWindow *ui;
};

#endif // TABLEDIALOUGEWINDOW_H

tabledialougewindow.cpp:

#include "tabledialougewindow.h"
#include "ui_tabledialougewindow.h"
#include "tablemainwindow1.h"
#include "ui_tablemainwindow1.h"

int Rows = 3;
int Columns = 3;
Ui::TableMainWindow1 *mainui;
TableDialougeWindow::TableDialougeWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TableDialougeWindow)
{
    ui->setupUi(this);
    ui->tableWidget->setRowCount(Rows);
    ui->tableWidget->setColumnCount(Columns);
}

TableDialougeWindow::~TableDialougeWindow()
{
    delete ui;
}


void TableDialougeWindow::on_buttonBox_accepted()
{
    for(int i = 0;i<Columns;++i){
        for(int j = 0;j<Rows;++j){
            QTableWidgetItem *celltxt = ui->tableWidget->item(j,i);
            QTableWidgetItem *celltxt2 =new QTableWidgetItem(*celltxt);
            mainui->tableWidget->setItem(j,i,celltxt2);
        }
    }
    accept();
}

hope this helps.

1 Answer 1

0

The error is we cannot insert an item that is already owned by another QTableWidget. So before make a copy we need to clone the data. we can use copy constructor to achieve this. Code comment will explain in detail. Hope this help.

    void MainWindow::on_cmdTransfer_clicked()
{
    //Source Table
    int rows = ui->tbl1->rowCount();
    int columns =ui->tbl1->columnCount();

    //Destination Table
    ui->tbl2->setColumnCount(columns);
    ui->tbl2->setRowCount(rows);

   //Copy data form one table to another.
   for (int i = 0; i<columns;++i){
       for (int j = 0;j<rows;++j){
         QTableWidgetItem *celltxt= ui->tbl1->item(j,i);
         //Clone the data using copy constructor
         QTableWidgetItem *celltxt1=new QTableWidgetItem(*celltxt);
         ui->tbl2->setItem(j,i,celltxt1);
       }
    }
}

enter image description here

9
  • Wow thank you so much! I was trying to think of copying and pasting it but was not too sure on how too do it with qt, thank you so much! Feb 6, 2016 at 17:01
  • -UPDATE- I tried inputting this code, but my program still crashes (and when i say crashes, i mean it compiles and runs the program, but when i click on the button to copy from one table to another it just says "Program.exe has stopped working"). Is it because one table is on a mainwindow screen and the other table is in a dialogue screen? Feb 6, 2016 at 17:08
  • @KittyBurner, Without seeing you class (Mainwindow & Dialog) structure, it is tough to say how to refer the object from one to another.
    – Jeet
    Feb 6, 2016 at 17:14
  • okay, can you tell me what exactly you want to see? cause when you say without seeing my class structure, im not sure exactly what part you are referring too. Feb 6, 2016 at 17:24
  • Is it possible to you to create and share a small implementation of your both the classes with the qtable(static) data, which gives error on copy? If so i can try from my side to find and rectify the error.
    – Jeet
    Feb 6, 2016 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.