Qt将数据从主窗口发送到辅助窗口

时间:2017-09-17 18:44:35

标签: c++ qt signals-slots

在我的主窗口中,我创建了一个名为PieceButton的自定义小部件的2d QVector。我现在想要从一个名为puzzle的不同窗口访问2d QVector。我无法连接2个表单进行数据传输。我在2d QVector填充之前创建了我正在使用的拼图,但是我所看到的一切都说明了连接语句进入拼图的构造函数(第二个窗口),这意味着向量需要准备好之前这个难题就是创造出来的。

我也尝试将connect函数放入一个在主窗口内调用的新函数,但是数据仍然没有传输,所以我把它放回到构造函数中;但这是更好的方法吗?

mainwindow.cpp

QVector<QVector<PieceButton*>> tilesVec;
...
void MainWindow::on_solveButton_clicked(){
for(int i = 0; i < 3; i++)
{
    x = 0;
    QVector<PieceButton*> row;

    for(int j = 0; j < 3; j++)
    {
        ...
        PieceButton *piece = new PieceButton(myPuzzle);
        ...
        row.push_back(piece);
    }
    tilesVec.push_back(row);
}
myPuzzle->show();
emit sendTileData(tilesVec);
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "puzzle.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
signals:
    void sendTileData(QVector<QVector<PieceButton*>> tilesVec);

private slots:
    void on_solveButton_clicked();

private:
    Ui::MainWindow *ui;
    Puzzle *myPuzzle;

};

#endif // MAINWINDOW_H

puzzle.cpp

QVector<QVector<PieceButton*>> tiles;

Puzzle::Puzzle(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Puzzle)
{
    ui->setupUi(this);
    this->setWindowTitle("Puzzle");
    resize(QDesktopWidget().availableGeometry(this).size() * 0.7);

    MainWindow *main;
    connect(main, SIGNAL(sendTileData(QVector<QVector<PieceButton*>>)), 
        this, SLOT(getTileData(QVector<QVector<PieceButton*>>)));

    int count = tiles.count();
    qDebug("%d", count); //this should be greater than 0, but is 0
}

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

void Puzzle::getTileData(QVector<QVector<PieceButton*>> sentTilesVec)
{
    tiles = sentTilesVec;
}

puzzle.h

namespace Ui {
class Puzzle;
}

class Puzzle : public QDialog
{
    Q_OBJECT

public:
    explicit Puzzle(QWidget *parent = 0);
    ~Puzzle();
    void connecting();
public slots:
    void getTileData(QVector<QVector<PieceButton*>> tilesVec);

private slots:
    void testSlot();


private:
    Ui::Puzzle *ui;
};

我试图只为您提供代码的相关部分。问题出在puzzle.cpp中的connect语句中,但我对如何解决它感到很遗憾。任何方向都会受到赞赏。

0 个答案:

没有答案
相关问题