简体   繁体   中英

how to fill qvector which is member of MainWindow by means of qtablewidget from Qdialog

I created MainWindow class by using QT Creator. Then I added some pushbutton to MainWindow to open a new dialog which I created form class by using QT Form Designer.

When a user clicks on the button a new dialog opens. The new dialog has a QTableWidget and a pushbutton . The user can fill QTableWidget with some values and after clicked the dialog's pushbutton I would like to send these values to MainWindow to fill QVector x and QVector y which are attributes of MainWindow class.

How can I achieve that? How can I get an instance of MainWindow from QDialog ? What would I write inside the function:

void Dialog::on_pushButton_clicked() {
    // Code Here
}

Thank you for your help in advance,

Peter below the code of my two classes

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "dialog.h"
#include <QVector>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow() override;

private:
    Ui::MainWindow *ui;
    Dialog *geometria;
    QVector<double> x;
    QVector<double> y;

private slots:
 void generate();

};

#endif // MAINWINDOW_H 
////////////////////////////////////////

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
geometria=new Dialog(this);
   connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(generuj_przekroj()));

}

MainWindow::~MainWindow()
{

    delete ui;

}

 void MainWindow::generate()
 {

     geometria->show();

 }

////////////////////////////////////

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();
 public slots:
    void wcisnij();
    void  wstaw_wiersz();
    bool  eventFilter(QObject *target, QEvent *event);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Dialog *ui;


};

#endif // DIALOG_H

/////////////////////////////////////////

#include "dialog.h"
#include "ui_dialog.h"
#include <QKeyEvent>
#include "xy.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
        ui->tableWidget->setRowCount(10);
        ui->tableWidget->setColumnCount(2);
        ui->tableWidget->setCurrentCell(4,1);
        ui->tableWidget->installEventFilter(this);

    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(wcisnij()));
    connect(ui->tableWidget,SIGNAL(itemSelectionChanged()),this,SLOT(wcisnij()));
    connect(ui->tableWidget,SIGNAL(itemSelectionChanged()),this,SLOT(wstaw_wiersz()));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::wcisnij()
{

   int r=ui->tableWidget->currentRow();
   int c=ui->tableWidget->currentColumn();
}

void  Dialog::wstaw_wiersz()
{
    int n=0;
    n=ui->tableWidget->rowCount();
    if(ui->tableWidget->currentRow()==n-1)
    ui->tableWidget->setRowCount(n+1);
}

bool  Dialog::eventFilter(QObject *target, QEvent *event) {

if (target==ui->tableWidget && event->type() == QEvent::KeyPress) {

      QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
      if( keyEvent->key() == Qt::Key_Return|| keyEvent->key() == Qt::Key_Down )
      {
                  int n=0;
                  int k=0;
                  n=ui->tableWidget->currentRow();
                  k=ui->tableWidget->currentColumn();
                  ui->tableWidget->setCurrentCell(n+1,k);

      }

      if( keyEvent->key() == Qt::Key_Up )
      {
                  int n=0;
                  int k=0;
                  n=ui->tableWidget->currentRow();
                  k=ui->tableWidget->currentColumn();
                  ui->tableWidget->setCurrentCell(n-1,k);

      }

    return true;

}

else
     return QObject::eventFilter(target, event);

}


void Dialog::on_pushButton_clicked()
{

}

The short answer is to load the data in your Dialog, and then emit a signal to MainWindow that it is ready, and let MainWindow get the data.
Add to Dialog.h:

public:
    std::vector<float> getX() { return x; }
    std::vector<float> getY() { return y; }

signals:
    dataReady();

private:
    QVector<double> x;
    QVector<double> y;

Add to Dialog.cpp:

void Dialog::on_pushButton_clicked()
    for (int r=0; r<rows; ++r)
        for (int c=0; c<rows; ++c) {
            QTableWidgetItem cell = tableWidget->item(r, c);
            QString contents = cell.text();
            double val = contents.toDouble();
            x.push_back(val);
        }

    //  And do the same to collect y data

    emit dataReady();

Add to MainWindow.h

private slots:
    void onDataReady();

Add to MainWindow.cpp

    MainWindow::MainWindow(...etc
{ ...
    geometria=new Dialog(this);

    connect(*geometria, SIGNAL(dataReady()), this, SLOT(onDataReady()));
    ...
}

        MainWindow::onDataReady()
{
    x = geometria->getX();
    y = geometria->getY();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM