简体   繁体   English

Qt-将QTableWidget的数据更改为另一个类的QTableWidget的数据

[英]Qt - Changing QTableWidget's Data to another class's QTableWidget's Data

I'm have two , one on a MainWindow ui and another on a Dialog ui. 我有两个,一个在MainWindow ui上,另一个在Dialog ui上。 What I'm trying to do is copy the data from the Dialog's and paste it in the MainWindow's . 我想做的是从Dialog复制数据并将其粘贴到MainWindow的中。 What I have tried to do is use Qt's Slot's and Signals mechanism to copy the data. 我试图做的是使用Qt的插槽和信号机制来复制数据。 Below is the code I tried to use: 以下是我尝试使用的代码:

MainWindow.h: MainWindow.h:

#include "ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT

public:   
explicit MainWindow(QWidget *parent = 0);


~MainWindow();

public slots:
void SetTableDataSlot(QTableWidgetItem* EditData);
}

MainWindow.cpp: MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
Dialog *dialog;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
dialog = new Dialog(this);
connect(mEdit,SIGNAL(setTableDataSignal(QTableWidgetItem*)),
                 this,SLOT(setTableDataSlot(QTableWidgetItem*)));
dialog->show();
}

void MainWindow::setTableDataSlot(QTableWidgetItem* EditData){
for(int i = 0; i<ui->tableWidget->rowCount();++i){
    for(int j = 0; j<ui->tableWidget->columnCount();++j){
        ui->tableWidget->setItem(i,j,EditData->clone());
    }
  }
}

Dialog.h: Dialog.h:

#include <QDialog>
#include <QTableWidget>


namespace Ui {
class dialog;
}

class dialog : public QDialog
{
Q_OBJECT

public:
explicit dialog(QWidget *parent = 0);

~dialog();
signals:
void setTableDataSignal(QTableWidgetItem *EditData);
}

Dialog.cpp: Dialog.cpp:

#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "editmode1.h"
#include "ui_editmode1.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}

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

void Dialog::on_pushButton_clicked()
{
for (int i=0; i<rows;++i){
    for(int j = 0; j<columns;++j){
        emit setTableDataSignal(ui->tableWidget->item(i,j);
    }
  }
}

Now the program compiles fine but when I click on the button on the dialog form, it copies the data from the last cell in the dialog table, and pastes it in every cell in the mainwindow table. 现在程序可以正常编译,但是当我单击对话框窗体上的按钮时,它会复制对话框表中最后一个单元格中的数据,并将其粘贴到mainwindow表中的每个单元格中。 If anyone can give me an explanation on what is wrong here and how it could be fixed I would be very grateful. 如果有人能给我解释一下这里出了什么问题以及如何解决,我将不胜感激。 Thanks in advance. 提前致谢。

Your error is in this line: 您的错误在以下行中:

ui->tableWidget->setItem(i,j,&EditData);

You pass a pointer of an object that live in the stack and will be destroyed at the end of the setTableDataSlot . 您传递的对象的指针位于堆栈中,并且将在setTableDataSlot的末尾销毁。 Also you are trying to set the same object in all the table cells. 另外,您正在尝试在所有表单元格中设置相同的对象。

You have to create a new QTableWidgetItem for each cell. 您必须为每个单元格创建一个新的QTableWidgetItem

Also the logic is wrong, because you are copying each cell of table A end put each copy into all cells of table B (cell [0, 0] of A in all cells of B, than cell [0, 1] of A in all cells of B and so on). 逻辑也错了,因为要复制表A的每个单元格,然后将每个副本放入表B的所有单元格中(B的所有单元格中A的单元格[0,0],而不是A中的单元格[0,1] B的所有单元格,依此类推)。

I suggest to change approach (for example get the table of the dialog from the main window and copy the cells, without passing all the QTableWidgetItem by signal-slot) 我建议更改方法(例如,从主窗口获取对话框的表并复制单元格,而不通过信号槽传递所有QTableWidgetItem)

EDIT 编辑

You can use this function to copy the QTableItems from a table to another (not tested) 您可以使用此功能将QTableItems从一个表复制到另一个表(未经测试)

void copyTableContents(QTableWidget* sourceTable, QTableWidget* destTable)
{
    for(int r = 0; r < sourceTable->rowCount() && r < destTable->rowCount(); ++r)
    {
        for(int c = 0; c < sourceTable->columnCount() && c < destTable->columnCount(); ++c)
        {
            //delete old item if exists
            QTableWidgetItem* dItem = destTable->takeItem(r, c);
            if(dItem)
                delete dItem;
            QTableWidgetItem* sItem = sourceTable->item(r, c);
            if(sItem) //copy the item if exists
                destTable->setItem(r, c, sItem->clone());
        }
    }
}

You can create a function in the dialog that return the table pointer. 您可以在对话框中创建一个返回表指针的函数。 You can emit a signal without parameters from dialog when the copy button is clicked. 单击复制按钮时,您可以从对话框发出不带参数的信号。 Connect this signal to a slot of the main window that get the table from the dialog and call the function to copy the items. 将此信号连接到主窗口的一个插槽,该插槽从对话框中获取表格并调用该函数以复制项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM