2010-06-08 39 views

回答

8

你需要signals and slots.

你必须点击的信号连接到一个自定义的插槽,通过您创建的主要部件的。

更正的代码,基于Patrice BernassolaJob的评论。

在类定义文件(.h文件)中添加行:

Q_OBJECT 

private slots: 
    void exampleButtonClicked(); 
private: 
    QDialog *exampleDialog; 

当你在你的类定义信号和槽需要宏Q_OBJECT。

变量exampleDialog应该在定义文件中声明为可以在槽中访问它。

而且你必须初始化它,这在构造

ExampleClass::ExampleClass() 
{ 
    //Setup you UI 
    dialog = new QDialog; 
} 

类实现(.cpp文件)中添加代码,你想要做什么的通常做法,在这种情况下创建一个新的窗口。

void ExampleClass::exampleButtonClicked() 
{ 
    exampleDialog->show(); 
} 

,你也必须将信号连接到与线插槽:

connect(exampleButton, SIGNAL(clicked()), this, SLOT(exampleButtonClicked())); 

你的问题是somehwat基本的,所以我建议阅读基础教程,通过这种方式可以使进步更快,避免等待答案。 一些链接教程,是对我很有用:

http://zetcode.com/tutorials/qt4tutorial/

http://doc.qt.io/archives/qt-4.7/tutorials-addressbook.html

+1

在你的例子中,新的对话框将被显示并立即被破坏,因为exampleDialog将在函数结束时超出范围。使你的对话框模态或使用成员变量来允许对话框活出这个功能。 – 2010-06-08 06:18:18

+0

或者只是使用QDialog :: exec() – Job 2010-06-08 15:15:10

+0

感谢您的更正。也忘记了Q_OBJECT宏。 它已被更正。 – 2010-06-08 17:27:49