[英]Accessing QTableWidget's data from another class
I've got a child widget (it's a configuration dialog to my MainWindow
) with a QTableWidget
on it. 我有一个带有
QTableWidget
的子窗口小部件(这是MainWindow
的配置对话框)。
panelSettingsDialog.h: panelSettingsDialog.h:
public:
explicit PanelSettingsDialog(QWidget *parent = 0);
~PanelSettingsDialog();
public:
QTableWidget *tableWidget;
private:
PanelSettingsDialog *panelSettingsDialog;
panelSettingsDialog.cpp: 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). tableWidget
按预期填充(我添加了5条虚拟数据行)。
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()
: 现在,我打算通过使用
panelSettings->tableWidget->rowCount()
语句遍历每一行,从MainWindow
类访问此有用的QTableWidget
信息,但是在尝试使用rowCount()
时出现读取访问错误:
mainwindow.cpp: 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). 通过以下方式在线程0中停止:在0x64098ffa处发生异常,代码:0xc0000005:在0x0处发生读取访问冲突,标志= 0x0(第一次机会)。
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? 如果我无法从另一个子类的窗口小部件项目中读取数据,那么将
QTableWidget
数据传递到MainWindow
以便对其进行迭代的一种好方法是什么?
@Chernobyl , perhaps you may have the answer to this. @Chernobyl,也许您可能对此有答案。
tableWidget
should be private. tableWidget
应该是私有的。 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. 当我们使用Qt Designer编写应用程序时,我们总是在私有部分使用
ui
,因为如果将其公共使用,则会遇到问题。 We should separate this things. 我们应该分开这件事。 tableWidget should be private, but we should provide some public methods which will do what we want.
tableWidget应该是私有的,但是我们应该提供一些公共方法来实现我们想要的功能。 I think you can use getters and setters.
我认为您可以使用getter和setter。
For example: 例如:
dialog.h dialog.h
public:
int getRowCountData();
dialog.cpp 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
. 您会看到
7
。 And so on with other things. 以此类推。
Edit (same structure): 编辑(结构相同):
QString getCellData(int row,int col);//in header
In .cpp 在.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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.