使用 qvector 将 QTableWidgetItems 从一个 tableWidget 转移到另一个
Transferring QTableWidgetItems from one tableWidget to another using a qvector
我的程序 运行 遇到了一个问题,我不知道如何解决。问题是我在两个 class 中有一个 Qtablewidget,我使用信号和槽机制来传输 QVector,它包含来自一个 class 的 QTablewidgetItems,并将它放在另一个 classes QTableWidget。我遇到的问题是,当我按下一个 class 上的传输按钮时,没有传输任何数据。谁能告诉我问题出在哪里?因为我被难住了。代码如下:
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editmode1.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
Ui::MainWindow *ui;
public slots:
void setTableDataSlot(int width, int height, QVector<QTableWidgetItem*>const& EditData);
private slots:
void on_actionEdit_Mode_triggered();
private:
EditMode1 *mEdit;
};
#endif // MAINWINDOW_H
EditMode1.h:
#ifndef EDITMODE1_H
#define EDITMODE1_H
#include <QDialog>
#include <QTableWidget>
namespace Ui {
class EditMode1;
}
class EditMode1 : public QDialog
{
Q_OBJECT
public:
explicit EditMode1(QWidget *parent = 0);
~EditMode1();
signals:
void setTableDataSignal(int width, int height, QVector<QTableWidgetItem*> const& EditData)
private slots:
void on_buttonBox_accepted();
void on_buttonBox_rejected();
private:
Ui::EditMode1 *Editui;
};
#endif // EDITMODE1_H
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QCoreApplication>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->setRowCount(6);
ui->tableWidget->setColumnCount(5);
for(int i = 0;i<ui->tableWidget->rowCount();++i){
for(int j = 0;j<ui->tableWidget->columnCount();++j){
QTableWidgetItem *tableitem = new QTableWidgetItem(" ");
ui->tableWidget->setItem(i,j,tableitem);
}
}
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_actionEdit_Mode_triggered()
{
mEdit = new EditMode1(this);
connect(mEdit,SIGNAL (setTableDataSignal(int width, int height, QVector<QTableWidgetItem*> const& EditData)),this,
SLOT(setTableDataSlot(int width, int height,QVector<QTableWidgetItem*>const& EditData)));
mEdit->show();
}
void MainWindow::setTableDataSlot(int width, int height,QVector<QTableWidgetItem*>const& EditData){
for(int i = 0; i<height;++i){
for(int j = 0; j<width;++j){
QTableWidgetItem* copieditem = new QTableWidgetItem( *EditData[i*height + j]) ;
ui->tableWidget->setItem(i,j,copieditem);
}
}
}
EditMode1.cpp:
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "editmode1.h"
#include "ui_editmode1.h"
int rows = 6;
int columns = 5;
EditMode1::EditMode1(QWidget *parent) :
QDialog(parent),
Editui(new Ui::EditMode1)
{
Editui->setupUi(this);
for (int i=0; i<rows;++i){
for(int j = 0; j<columns;++j){
QTableWidgetItem *blankitem = new QTableWidgetItem(" ");
Editui->tableWidget->setItem(i,j,blankitem);
}
}
}
EditMode1::~EditMode1()
{
delete Editui;
}
void EditMode1::on_buttonBox_accepted()
{
QVector <QTableWidgetItem*> AllItems;
for (int i=0; i<rows;++i){
for(int j = 0; j<columns;++j){
AllItems.push_back(Editui->tableWidget->item(i,j));
}
}
emit setTableDataSignal(columns,rows,AllItems);
}
void EditMode1::on_buttonBox_rejected()
{
reject();
}
很抱歉有很多代码,但我不知道程序的哪一部分导致它什么也没产生,我认为显示所有代码会让其他人更容易调试。提前致谢!
您可能想用数据表示区域维度,因此信号应该类似于:
signals:
void setTableDataSignal(
int width, int height, const QVector<QTableWidgetItem*>& EditData);
发送信号试试:
void EditMode1::on_buttonBox_accepted()
{
QVector <QTableWidgetItem*> AllItems;
for (int y = 0; y < rows; ++y )
{
for( int x = 0; x < columns; ++x )
{
AllItems.push_back( Editui->tableWidget->item(x,y) );
}
}
emit setTableDataSignal( columns, rows, AllItems );
}
在接收小部件中,您应该执行如下操作:
void MainWindow::setTableDataSlot(
int width, int height, const QVector<QTableWidgetItem*>& EditData)
{
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
QTableWidgetItem* copieditem = new QTableWidgetItem( *EditData[y*height + x]) ;
ui->tableWidget->setItem( x, y, copieditem );
}
}
}
我的程序 运行 遇到了一个问题,我不知道如何解决。问题是我在两个 class 中有一个 Qtablewidget,我使用信号和槽机制来传输 QVector,它包含来自一个 class 的 QTablewidgetItems,并将它放在另一个 classes QTableWidget。我遇到的问题是,当我按下一个 class 上的传输按钮时,没有传输任何数据。谁能告诉我问题出在哪里?因为我被难住了。代码如下:
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editmode1.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
Ui::MainWindow *ui;
public slots:
void setTableDataSlot(int width, int height, QVector<QTableWidgetItem*>const& EditData);
private slots:
void on_actionEdit_Mode_triggered();
private:
EditMode1 *mEdit;
};
#endif // MAINWINDOW_H
EditMode1.h:
#ifndef EDITMODE1_H
#define EDITMODE1_H
#include <QDialog>
#include <QTableWidget>
namespace Ui {
class EditMode1;
}
class EditMode1 : public QDialog
{
Q_OBJECT
public:
explicit EditMode1(QWidget *parent = 0);
~EditMode1();
signals:
void setTableDataSignal(int width, int height, QVector<QTableWidgetItem*> const& EditData)
private slots:
void on_buttonBox_accepted();
void on_buttonBox_rejected();
private:
Ui::EditMode1 *Editui;
};
#endif // EDITMODE1_H
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QCoreApplication>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->setRowCount(6);
ui->tableWidget->setColumnCount(5);
for(int i = 0;i<ui->tableWidget->rowCount();++i){
for(int j = 0;j<ui->tableWidget->columnCount();++j){
QTableWidgetItem *tableitem = new QTableWidgetItem(" ");
ui->tableWidget->setItem(i,j,tableitem);
}
}
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_actionEdit_Mode_triggered()
{
mEdit = new EditMode1(this);
connect(mEdit,SIGNAL (setTableDataSignal(int width, int height, QVector<QTableWidgetItem*> const& EditData)),this,
SLOT(setTableDataSlot(int width, int height,QVector<QTableWidgetItem*>const& EditData)));
mEdit->show();
}
void MainWindow::setTableDataSlot(int width, int height,QVector<QTableWidgetItem*>const& EditData){
for(int i = 0; i<height;++i){
for(int j = 0; j<width;++j){
QTableWidgetItem* copieditem = new QTableWidgetItem( *EditData[i*height + j]) ;
ui->tableWidget->setItem(i,j,copieditem);
}
}
}
EditMode1.cpp:
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "editmode1.h"
#include "ui_editmode1.h"
int rows = 6;
int columns = 5;
EditMode1::EditMode1(QWidget *parent) :
QDialog(parent),
Editui(new Ui::EditMode1)
{
Editui->setupUi(this);
for (int i=0; i<rows;++i){
for(int j = 0; j<columns;++j){
QTableWidgetItem *blankitem = new QTableWidgetItem(" ");
Editui->tableWidget->setItem(i,j,blankitem);
}
}
}
EditMode1::~EditMode1()
{
delete Editui;
}
void EditMode1::on_buttonBox_accepted()
{
QVector <QTableWidgetItem*> AllItems;
for (int i=0; i<rows;++i){
for(int j = 0; j<columns;++j){
AllItems.push_back(Editui->tableWidget->item(i,j));
}
}
emit setTableDataSignal(columns,rows,AllItems);
}
void EditMode1::on_buttonBox_rejected()
{
reject();
}
很抱歉有很多代码,但我不知道程序的哪一部分导致它什么也没产生,我认为显示所有代码会让其他人更容易调试。提前致谢!
您可能想用数据表示区域维度,因此信号应该类似于:
signals:
void setTableDataSignal(
int width, int height, const QVector<QTableWidgetItem*>& EditData);
发送信号试试:
void EditMode1::on_buttonBox_accepted()
{
QVector <QTableWidgetItem*> AllItems;
for (int y = 0; y < rows; ++y )
{
for( int x = 0; x < columns; ++x )
{
AllItems.push_back( Editui->tableWidget->item(x,y) );
}
}
emit setTableDataSignal( columns, rows, AllItems );
}
在接收小部件中,您应该执行如下操作:
void MainWindow::setTableDataSlot(
int width, int height, const QVector<QTableWidgetItem*>& EditData)
{
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
QTableWidgetItem* copieditem = new QTableWidgetItem( *EditData[y*height + x]) ;
ui->tableWidget->setItem( x, y, copieditem );
}
}
}