You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, April 11th 2009, 10:13pm

One problem with scope :/

Hello all!

I'm a completly newby in QT. I've a little problem:

I've made a two files:
main.cpp

Source code

1
2
3
4
5
6
7
8
9
10
#include <QtGui/QApplication> 
#include "widget.h"  

int main(int argc, char *argv[]) 
{     
QApplication a(argc, argv);
Widget w; 
    w.show();     
return a.exec(); 
}


widget.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "widget.h" 
#include "ui_widget.h" 
#include <QtGui>  
Widget::Widget(QWidget *parent)     : QWidget(parent), ui(new Ui::WidgetClass) {
 ui->setupUi(this);     
connect( btn1, SIGNAL( clicked() ), this, SLOT( getPath() ) ); 
}  

void Widget::getPath() {     
QString path;      path = QFileDialog::getOpenFileName(         this,         "Choose a file to open",         QString::null,         QString::null); 
}   
Widget::~Widget() {     
delete ui; 
}


and finaly widget.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef WIDGET_H 
#define WIDGET_H 
 #include <QtGui/QWidget>  
namespace Ui {     class WidgetClass; } 

class Widget : public QWidget { 
    Q_OBJECT  
public:     
Widget(QWidget *parent = 0);     
~Widget(); 

public slots:     
void getPath(); 

private:
 Ui::WidgetClass *ui; 
};
 #endif // WIDGET_H 
There is a one button (name btn1) on the form (widget.ui).
When I'm trying compile my application a get an error:

Quoted

C:/qt_workspace/xxxxx/widget.cpp:9: error: `btn1' was not declared in this scope
What I should do, for btn1 can be "visible" in file widget.cpp?

Thank you very much for your time.

2

Sunday, April 12th 2009, 6:43am

Hi,
read generated ui_widget.h file. You will see how setupUi works. More - http://doc.trolltech.com/4.5/designer-using-a-ui-file.html

Source code

1
connect(ui->btn1, ...
Fighting fire with fire.
Three can keep a secret if two of them are dead.