Accessing QTableWidget's data from another class

穿精又带淫゛_ 提交于 2019-12-11 10:52:02

问题


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.


回答1:


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";
}


来源:https://stackoverflow.com/questions/25872862/accessing-qtablewidgets-data-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!