Qt - Copying a QTableWidget's Data and Paste it in Another QTableWidget
<p>I asked a question very similar to this  but the person who responded gave a small amount of help and I couldn't solve my problem. I thought I would re-ask the question since no-one else has responded to my problem. I would like to know how to copy a QTableWidget's Data from one window to another QTableWidget on a different window. </p>

<p>So for example, I would type into "TableWidget1", then with the click of a button, It copies the data from "TableWidget1" and pastes it in "TableWidget2" on a different window.  I have code below which I tried using and when I compiled the program, it worked fine until the copy and paste button was clicked, then it would just crash.</p>

<p>Here is the code (there is a lot, sorry):</p>

<p>tablemainwindow1.h:</p>

<pre><code>#ifndef TABLEMAINWINDOW1_H
#define TABLEMAINWINDOW1_H

#include &lt;QMainWindow&gt;
#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
</code></pre>

<p>tablemainwindow1.cpp:</p>

<pre><code>#include "tablemainwindow1.h"
#include "ui_tablemainwindow1.h"

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

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


void TableMainWindow1::on_pushButton_clicked()
{
    tbl2 = new TableDialougeWindow(this);
    tbl2-&gt;show();
}
</code></pre>

<p>tabledialougewindow.h:</p>

<pre><code>#ifndef TABLEDIALOUGEWINDOW_H
#define TABLEDIALOUGEWINDOW_H

#include &lt;QDialog&gt;

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
</code></pre>

<p>tabledialougewindow.cpp:</p>

<pre><code>#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-&gt;setupUi(this);
    ui-&gt;tableWidget-&gt;setRowCount(Rows);
    ui-&gt;tableWidget-&gt;setColumnCount(Columns);
}

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


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

<p>Any and all help is appreciated. thank you. </p>