从另一个类访问QTableWidget的数据 [英] Accessing QTableWidget's data from another class
问题描述
我有一个子窗口小部件(它是一个配置对话框到我的 MainWindow
)与 QTableWidget
。
panelSettingsDialog.h:
public:
explicit PanelSettingsDialog(QWidget * parent = 0);
〜PanelSettingsDialog();
public:
QTableWidget * tableWidget;
private:
PanelSettingsDialog * panelSettingsDialog;
panelSettingsDialog.cpp:
#includepanelsettingsdialog.h
#includeui_panelsettingsdialog.h
#include< QCheckBox>
PanelSettingsDialog :: PanelSettingsDialog(QWidget * parent):
QDialog(parent),
ui(new Ui :: PanelSettingsDialog)
{
ui-> setupUi(this);
setWindowTitle(Channel Settings& Panel Configuration);
tableWidget = new QTableWidget(this);
tableWidget
现在我打算从我的访问这个有用的
类,通过使用 QTableWidget
信息> MainWindow panelSettings-> tableWidget-> rowCount()
语句迭代每一行,尝试使用 rowCount()
时出现访问错误:
mainwindow.cpp:
void MainWindow :: configure_panels()
{
const int totalRowCount = panelSettingsDialog-> tableWidget-> rowCount();
}
会出现以下错误讯息:
在线程0中停止:0x64098ffa处的异常,代码:0xc0000005:读取访问违例:0x0,flags = 0x0(第一次机会)。
如果我无法从另一个子类的窗口项中读取,那么将 QTableWidget
数据传递到 MainWindow
,以便它可以迭代通过?
@Chernobyl,也许你可能有这个答案。
tableWidget
应该是私有的。当我们使用Qt Designer编写应用程序时,我们总是在私有部分使用我们的 ui
,因为如果我们使用它作为public,我们可以得到问题。我们应该分开这件事。 tableWidget应该是私有的,但我们应该提供一些公共方法,这将做我们想要的。我想你可以使用getter和setter。
例如:
dialog.h
public:
int getRowCountData();
dialog.cpp
int Dialog :: getRowCountData()
{
return ui-> tableWidget-> rowCount();
}
// ...在构造函数中的某处
ui-> tableWidget-> setColumnCount(1);
for(int r = 0; r <7; r ++)
{
ui-> tableWidget-> insertRow(r);
ui-> tableWidget-> setCellWidget(r,0,new QCheckBox(QString(checkBox%1)。arg(r)));
}
用法:
void MainWindow :: on_newForm_clicked()
{
Dialog * mDialog = new Dialog;
mDialog-> setModal(true);
mDialog-> show();
qDebug()<< mDialog-> getRowCountData();
}
您会看到 7
。
编辑(结构相同):
QString getCellData(int row,int col); // in header
QString Dialog :: getCellData(int row,int col)
{
QCheckBox * curBox = qobject_cast& *>(ui-> tableWidget-> cellWidget(row,col));
if(curBox)
{
return curBox-> text();
}
return QString();
}
用法:
Dialog * mDialog = new Dialog;
mDialog-> show();
qDebug()<< mDialog-> getRowCountData();
for(int r = 0; r <7; r ++)
{
QString txt = mDialog-> getCellData(r,0);
if(!txt.isNull())
qDebug()<<文本;
else
qDebug()<< 失败;
}
I've got a child widget (it's a configuration dialog to my MainWindow
) with a QTableWidget
on it.
panelSettingsDialog.h:
public:
explicit PanelSettingsDialog(QWidget *parent = 0);
~PanelSettingsDialog();
public:
QTableWidget *tableWidget;
private:
PanelSettingsDialog *panelSettingsDialog;
panelSettingsDialog.cpp:
#include "panelsettingsdialog.h"
#include "ui_panelsettingsdialog.h"
#include <QCheckBox>
PanelSettingsDialog::PanelSettingsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::PanelSettingsDialog)
{
ui->setupUi(this);
setWindowTitle("Channel Settings & Panel Configuration");
tableWidget = new QTableWidget(this);
tableWidget
populates as expected (I added 5 dummy rows of data).
Now I intend to access this useful QTableWidget
information from my MainWindow
class by iterating through each of the rows, using the panelSettings->tableWidget->rowCount()
statement, but get a read access error when trying to use the rowCount()
:
mainwindow.cpp:
void MainWindow::configure_panels()
{
const int totalRowCount = panelSettingsDialog->tableWidget->rowCount();
}
breaks here, with the following error message:
Stopped in thread 0 by: Exception at 0x64098ffa, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance).
If I am unable to read from another child class's widget items, what would a good method be for passing QTableWidget
data to MainWindow
so that it can be iterated through?
@Chernobyl , perhaps you may have the answer to this.
tableWidget
should be private. When we write app with Qt Designer we always use our ui
in private section, because if we will use it as public, we can get problems. We should separate this things. tableWidget should be private, but we should provide some public methods which will do what we want. I think you can use getters and setters.
For example:
dialog.h
public:
int getRowCountData();
dialog.cpp
int Dialog::getRowCountData()
{
return ui->tableWidget->rowCount();
}
//... somewhere in constructor
ui->tableWidget->setColumnCount(1);
for(int r=0;r<7;r++)
{
ui->tableWidget->insertRow(r);
ui->tableWidget->setCellWidget(r,0,new QCheckBox(QString("checkBox%1").arg(r)));
}
Usage:
void MainWindow::on_newForm_clicked()
{
Dialog *mDialog = new Dialog;
mDialog->setModal(true);
mDialog->show();
qDebug() << mDialog->getRowCountData();
}
You'll see 7
. And so on with other things.
Edit (same structure):
QString getCellData(int row,int col);//in header
In .cpp
QString Dialog::getCellData(int row, int col)
{
QCheckBox* curBox = qobject_cast<QCheckBox*>(ui->tableWidget->cellWidget(row,col));
if(curBox)
{
return curBox->text();
}
return QString();
}
Usage:
Dialog *mDialog = new Dialog;
mDialog->show();
qDebug() << mDialog->getRowCountData();
for(int r=0;r<7;r++)
{
QString txt = mDialog->getCellData(r,0);
if(!txt.isNull())
qDebug() << txt;
else
qDebug() << "fail";
}
这篇关于从另一个类访问QTableWidget的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!