2010-07-16 153 views
0

我不知道如何連接兩種形式,我可以打開第二種形式但無法訪問第一種形式。關於qt中的信號和插槽

我的程序:

#include<QApplication> 
#include<QLabel> 
#include<QPushButton> 
#include<QGridLayout> 

class form1 
{ 
    public: 
     QWidget *window1; 
     QLabel *l1; 
     QPushButton *b1; 
     QGridLayout *gl; 
     form1(){} 
     void setup1(); 
     void show1(); 


}; 

void form1::setup1() 
{ 
    window1=new QWidget(); 
    gl=new QGridLayout(window1); 
    l1=new QLabel("I AM IN FORM1",window1); 
    b1=new QPushButton("NEXT",window1); 
    gl->addWidget(l1,0,0); 
    gl->addWidget(b1,1,0); 
} 
void form1::show1() 
{ 
    window1->show(); 
} 

class form2 
{ 
    public: 
     QWidget *window2; 
     QPushButton *b2; 
     form2(){} 
     void setup2(); 
     void show2(); 

}; 

void form2::setup2() 
{ 
    window2=new QWidget(); 
    b2=new QPushButton("NEXT",window2); 
} 
void form2::show2() 
{ 
    window2->show(); 
} 


class Myclass:public QObject,public form1,public form2 
{ 


    public slots: 
     void open(); 
     void back(); 
    public: 
     Myclass() 
     { 
      setup1(); 
      setup2(); 
      QObject::connect(b1,SIGNAL(clicked()),window1,SLOT(open())); 
      QObject::connect(b2,SIGNAL(clicked()),window2,SLOT(back())); 
     } 


}; 

void Myclass::open() 
{ 
    //window1->hide(); 
    //window2->show(); 
    show2(); 
} 

void Myclass::back() 
{ 
    window2->hide(); 
    l1->setText("BACK FROM FORM2"); 
    window1->show(); 
} 

int main(int argc,char *argv[]) 
{ 
    QApplication app(argc,argv); 
    Myclass *m=new Myclass(); 
    m->show1(); 
    return app.exec(); 
} 
+0

請編輯您的問題,以便源代碼可讀。 – Lars 2010-07-16 10:18:09

回答

1

MHM怎麼做的第一個工作? open()和back()都屬於MyClass。但是你的連接去往window1和window2。嘗試它是否適用於「this」而不是window1/2。

0

當我開始你的程序。我得到這個運行時間qt warnigs:

Object::connect: No such slot QWidget::open() in question.h:44 
Object::connect: No such slot QWidget::back() in question.h:45 

所以你的連接無效。插槽在Myclass中不在window1和window2中定義。與

QObject::connect(b1,SIGNAL(clicked()),this,SLOT(open())); 
QObject::connect(b2,SIGNAL(clicked()),this,SLOT(back())); 

或更短更換:

connect(b1,SIGNAL(clicked()),this,SLOT(open())); 
connect(b2,SIGNAL(clicked()),this,SLOT(back())); 

,你會得到預期的行爲。