我遇到了我的程序问题,我不知道如何解决。问题是我在两个类中有一个Qtablewidget,我使用信号和插槽机制来传输一个QVector,它包含来自一个类的QTablewidgetItems,并将它放在其他类QTableWidget中。我得到的问题是,当我按一个类的传输按钮时,没有数据传输。谁能告诉我这是什么问题?因为我很难过。代码如下:
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();
}
很抱歉有很多代码,但我不知道程序的哪个部分导致它什么也没产生,我想在显示的所有代码中,其他人调试会更容易。提前谢谢!
答案 0 :(得分:1)
您可能希望用数据表示区域尺寸,因此信号应该类似于:
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 );
}
}
}