PDA

View Full Version : Another scope problem!



mikea
4th October 2014, 21:41
In the following code segment from dialog.cpp, the push button saveFolderPB gives the error "not declared in this scope". WHY? I have no idea. I have a simple dialog with a few buttons using on_object_triger private slots, which all work correctly. In the slot shown in the code snipped all I am doing is trying to enable another push button. Why is the button widget not declared in scope? It is public in ui_dialog.h, which is included as shown in the snipped - and it was put there by QCreator. I thought all class member variables are accessible by all class non-static member methods.


#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <iostream>
....

void Dialog::on_QFXSourcePB_pressed()
{
// Display file selection dialog.
QString selfilter = "QFX (*.qfx)";
QString inFileName = QFileDialog::getOpenFileName(
this,
"Select .qfx file.",
"C:\\users\\Mike\\Desktop",
"All files (*.*);; QFX (*.qfx)", &selfilter);

if (!inFileName.isEmpty()){
saveFolderPB.setEnabled(); // NOT DECLARED IN THIS SCOPE !!
QFXreader reader;
reader.readQFX(inFileName, qfxInfo, qifTL);
}

}

sedi
4th October 2014, 23:29
It may be public there, but you still have to specify where it is. Normally Qt gives you the ui-> namespace that will give you access to your ui elements like this:

ui->whateverWidget

For this to work you should find (automatically created)

Before your class definition (here it is my MainWindow) in the header file:


namespace Ui {
class YourClass;
}

Private in the header:

Ui::YourClass *ui;

In the .cpp the constructor should call
ui(new Ui::YourClass) and before acually using the ui you need a
ui->setupUi(this);

All of this should have been done automatically when using the Designer. Just use the ui-> thingy :-)

HTH
Sebastian

mikea
4th October 2014, 23:48
sedi, thanks for the reply. When I worked with Designer years ago, I was not concerned with any ui namespace that I can remember. And yes, your right QCreator should have put in the namespace declaration.
Mike

sedi
5th October 2014, 01:02
According to this thread (http://www.qtcentre.org/threads/60456-Not-declared-in-scope) of yours, it seems to actually have done so:


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QList>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{

mikea
5th October 2014, 02:15
Yea, I noticed that just after my last reply. I'll just remember to use the ui-> thingy!

anda_skoa
5th October 2014, 07:25
sedi, thanks for the reply. When I worked with Designer years ago, I was not concerned with any ui namespace that I can remember.

You were probably inheriting from the generated class



And yes, your right QCreator should have put in the namespace declaration.


That's what it does when you create a "Designer form class", i.e. it creates both the form and the class using it.

Cheers,
_