有权访问另一个类的Ui

时间:2013-06-16 13:03:53

标签: qt

我有一个名为FirstPage的类 我在主窗口中有一个FirstPage的对象! 现在我想要访问主窗口中FirstPage的“Ui” 但我不能!

    namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:

private:
    FirstPage* FrstPg;
};

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    FrstPg = new FirstPage (this);
    connect (FrstPg->ui->pushButton,SIGNAL(clicked(),FrstPg,SLOT(show());//Error ERRor
}

我该怎么办?!

2 个答案:

答案 0 :(得分:0)

为什么你转发在MainWindow命名空间中声明Ui类,但你的MainWindow声明不在此命名空间中?您最终拥有两个不同的MainWindow类,一个在全局命名空间中,另一个在Ui命名空间中。我想你要做的是:

namespace Ui {

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:

private:
    FirstPage* FrstPg;
};

}

同样的cpp文件:

namespace Ui {

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    FrstPg = new FirstPage (this);
    connect (FrstPg->ui->pushButton,SIGNAL(clicked(),FrstPg,SLOT(show());//Error ERRor
}

}

现在关于您的错误,为什么要尝试在MainWindow课程中进行连接,而不是在FirstPage课程中进行连接?您的信号和插槽都在这个类中,我认为没有任何理由在这里进行连接。有没有我错过的东西?

答案 1 :(得分:0)

要在FirstPage.h中从uiUi::FirstPage *ui;部分访问FirstPage的private ivar移动public

P.S。 Uflex关于命名空间的建议不正确:namespace Ui { class MainWindow; }用于MainWindow.h中Ui::MainWindow *ui;的前向声明

相关问题