Connecting Signals and Slots between Child and Parent (invalid use of incomplete type)

时间:2017-06-22 17:12:32

标签: qt

I'm trying to connect signals from a child window (QDialog named VolumePage) to its parent (QMainWindow named MockUI). I'm running into a:

invalid use of incomplete type 'class Ui::VolumePage'

error when I attempt to make the connection.

I'm trying to connect inside the MockUI.cpp when I make the volume page. (Happens on a button press)

 void MockUI::on_pushButton_3_clicked()
 {
 //Non-Modal Approach

 volPage = new VolumePage(this); 
 volPage->show();
 connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
 }

I've made Ui::VolumePage *ui; public.

Here's the entire error:

error: invalid use of incomplete type 'class Ui::VolumePage' connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));

Can anyone help me understand what the problem is, or another clean way to do what I'm trying to do?

Edit: (Additional Source)

VolumePage.h:

#ifndef VOLUMEPAGE_H
#define VOLUMEPAGE_H

#include <QDialog>

namespace Ui {
class VolumePage;
}

class VolumePage : public QDialog
{
    Q_OBJECT

public:
    explicit VolumePage(QWidget *parent = 0);
    Ui::VolumePage *ui;
    ~VolumePage();

private slots:
    void on_verticalSlider_valueChanged(int value);

private:

};

#endif // VOLUMEPAGE_H

mockUI.cpp:

#include "mockUI.h"
#include "ui_mock_ics.h"
#include <QLCDNumber>
#include "volumepage.h"

MockUI::MockUI(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MockUI)
{
    ui->setupUi(this);

}

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


void MockUI::on_pushButton_3_clicked()
{
    //Modal Approach
    //VolumePage volPage;
    //volPage.setModal(true);
    //volPage.exec();

    //Non-Modal Approach
    volPage = new VolumePage(this);    
    volPage->show();
    connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));

}

volumepage.cpp:

#include "volumepage.h"
#include "mockUI.h"
#include "ui_volumepage.h"

VolumePage::VolumePage(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VolumePage)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
    //Mock_ICS* ics = (Mock_ICS*)parent;
    //connect(ui->verticalSlider,SIGNAL(valueChanged(int)),ics->ui->progressBar,SLOT(setValue(int)));
    //connect(ui->verticalSlider,SIGNAL(valueChanged(int)),this->parent()->progressBar,SLOT(setValue(int)));


}

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

2 个答案:

答案 0 :(得分:0)

这是C ++中pImpl idiom的一个例子。类VolumePage是您应该使用的类 - 这是Qt Creator生成的“公共”接口。 Ui::VolumePage包含您不必担心的复杂细节。

因此,在您的代码中,您应该使用VolumePage,而不是Ui::VolumePage。你不应该公开Ui::VolumePage

答案 1 :(得分:0)

Ui::VolumePage *ui应该保密。而是在volPage上创建一个信号,如verticalSliderValueChanged(int value)。见下文......

mockui.h

#ifndef MOCKUI_H
#define MOCKUI_H

#include <QMainWindow>
#include "volumepage.h"

namespace Ui {
class MockUI;
}

class MockUI : public QMainWindow
{
    Q_OBJECT

public:
    explicit MockUI(QWidget *parent = 0);
    ~MockUI();
    VolumePage* volPage;

private slots:
    void on_pushButton_3_clicked();

private:
    Ui::MockUI *ui;
};

#endif // MOCKUI_H

mockui.cpp

#include "mockui.h"
#include "ui_mockui.h"
#include "volumepage.h"

MockUI::MockUI(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MockUI)
{
    ui->setupUi(this);
}

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


void MockUI::on_pushButton_3_clicked()
{
    //Non-Modal Approach
    volPage = new VolumePage(this);
    volPage->show();
    connect(volPage,SIGNAL(verticalSliderValueChanged(int)),ui->progressBar,SLOT(setValue(int)));

}

volumepage.h

#ifndef VOLUMEPAGE_H
#define VOLUMEPAGE_H

#include <QDialog>

namespace Ui {
class VolumePage;
}

class VolumePage : public QDialog
{
    Q_OBJECT

public:
    explicit VolumePage(QWidget *parent = 0);
    ~VolumePage();

signals:
    void verticalSliderValueChanged(int value);

private slots:
    void on_verticalSlider_valueChanged(int value);

private:
    Ui::VolumePage *ui;
};

#endif // VOLUMEPAGE_H

volumepage.cpp

#include "volumepage.h"
#include "ui_volumepage.h"

VolumePage::VolumePage(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VolumePage)
{
    ui->setupUi(this);
}

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

void VolumePage::on_verticalSlider_valueChanged(int value)
{
    emit verticalSliderValueChanged(value);
}