Error in subclassing QAbstractSlider
-
wrote on 2 Jun 2011, 08:35 last edited by
Hello,
In my application, I have one vertical scrollbar (named verticalScrollBar_z) and a QGraphicsScene (named Canvas* scene). So I have also subclassed the QAbstractSlider, in order to take the value of the scrollbar and use it in a function. But when I am debuging my code I get this error.
error C2065: 'verticalScrollBar_z' : undeclared identifier.Could somebody help me to solve the problem? What is wrong in my logic?////////// scrollbar.h //////////
@#ifndef SCROLLBAR_H
#define SCROLLBAR_H#include <QAbstractSlider>
class my_scrollbar : public QAbstractSlider
{
Q_OBJECTpublic:
my_scrollbar();void valueChanged (int);
signals:
void setValue (int value);
};#endif // SCROLLBAR_H@
////////// scrollbar.cpp //////
@#include "scrollbar.h"my_scrollbar::my_scrollbar()
{
}void my_scrollbar::valueChanged (int )
{
emit setValue(ui->verticalScrollBar_z->value());}@
//////// mainwindow.h //////////////
@#include "scrollbar.h"namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTCanvas *scene;
my_scrollbar *verticalScrollBar_z;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;public slots:
void setValue (int value);
};@///////// mainwindow.cpp /////////////
@#include "scrollbar.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{connect(ui->verticalScrollBar_z,SIGNAL(valueChanged(int)),
this,SLOT(setValue(int)));}
void MainWindow::setValue(int value)
{.....code....
start[2] = value;
}@
-
wrote on 2 Jun 2011, 08:44 last edited by
Did you promote the slider to my_scrollbar in Qt Designer?
-
wrote on 2 Jun 2011, 08:48 last edited by
What exactly do you mean?To set the scrollbar and give it the name of verticalScrollBar_z? If yes, this has been done
-
wrote on 2 Jun 2011, 09:01 last edited by
A proper way of subclassing widgets using ui forms is to choose one type that best ressembles your widget. In your case this is qslider. You give it a name you can remember myWidget and you right button click on it choose promote and give it the name of your subclass which is my_scrollbar (i would name it rather my_slider to make reading your code easier afterwards)
Then you can use it in your code like ui->myWidget.I suggest you read more about this approch in the docs.
Look for subclassing in Qt DesignerEdit :
" Try this link: ":http://doc.qt.nokia.com/4.7/designer-using-custom-widgets.html -
wrote on 2 Jun 2011, 09:34 last edited by
I have made this change to the code
@#ifndef SCROLLBAR_H
#define SCROLLBAR_H#include <QSlider>
class my_scrollbar : public QSlider
{
Q_OBJECTpublic:
my_scrollbar();void valueChanged (int);
signals:
void setValue (int value);
};#endif // SCROLLBAR_H@
But after choose the QSlider, my_scrollbar as Promoted class name and header file, then Qt Designer do not give me the choise of promote my_scrollbar. Do you know why? Do I must change something else?
-
wrote on 2 Jun 2011, 10:16 last edited by
I think you succeeded because you could set the class name and corresponding headerfile in Qt Designer.
Once promoted you cannot promote it again unless you un- promote it first.
Now the only thing you have to do is use it in your code like ui->myWidget. And you don't have t make an instance of your class anymore because you have it already.
-
wrote on 2 Jun 2011, 10:24 last edited by
You say that I do not to subclass my_scrollbar. I need just to promote myWidget, and use it in my code like this ui->myWidget.Also I have to use the
@connect(ui->verticalScrollBar_z,SIGNAL(valueChanged(int)),
this,SLOT(setValue(int)));@Had I understand it correct? Instead of QSlider could I use QScrollbar?
-
wrote on 2 Jun 2011, 10:30 last edited by
I didn't say that you don't have to subclass. I said you don't have to make an instance of your subclass anymore like you did in your mainwindow class.
You already have access to your slider that you subclassed by using
@Ui->myWidget@
Which points to your slider in your form.Edit:
I think you named your promoted widget :
@ui->verticalScrollBar_z@
So use this instead of my example. I was just explaining what to do using an example. i wasn't programming for you. -
wrote on 2 Jun 2011, 10:51 last edited by
I have used exactly the code I have included in the begin of this discuss. Also, I promoted verticalScrollBar_z as a QScrollBar with promoted class name and header file the my_scrollball. But still I get the same error. From what you said, it should work properly? Do you know what is the reason? I really cannot find my mistake.
-
wrote on 2 Jun 2011, 11:06 last edited by
What error do you get?
-
wrote on 2 Jun 2011, 11:09 last edited by
error C2065: ‘ui->’ : undeclared identifier
Is difficult to understand the procedure. Could you be more spesiphic, or give me a simple example? -
wrote on 2 Jun 2011, 11:18 last edited by
If you type @ui.@
Then ctrl + tab
What members can you use? Do you recognize the name you gave to your widget in Qt Designer? Then use it. What did you use? a QSlider? -
wrote on 2 Jun 2011, 11:52 last edited by
In the official Qt book :
bq. C++ GUI Programming with Qt 4
by Jasmin Blanchette; Mark Summerfield
If you google you should find it. Or you could use the docs .....Getting startedYou should read chapter 5 : Creating Custom Widgets
bq. Integrating Custom Widgets with Qt Designer
There the author explains with code and pictures the process of promoting widgets.
1/13