Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error in subclassing QAbstractSlider
Forum Updated to NodeBB v4.3 + New Features

Error in subclassing QAbstractSlider

Scheduled Pinned Locked Moved General and Desktop
13 Posts 2 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jk_mk
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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_OBJECT

     Canvas *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;

    }@

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Did you promote the slider to my_scrollbar in Qt Designer?

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jk_mk
        wrote on last edited by
        #3

        What exactly do you mean?To set the scrollbar and give it the name of verticalScrollBar_z? If yes, this has been done

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          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 Designer

          Edit :
          " Try this link: ":http://doc.qt.nokia.com/4.7/designer-using-custom-widgets.html

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jk_mk
            wrote on last edited by
            #5

            I have made this change to the code

            @#ifndef SCROLLBAR_H
            #define SCROLLBAR_H

            #include <QSlider>

            class my_scrollbar : public QSlider
            {
            Q_OBJECT

            public:
            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?

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              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.

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jk_mk
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0
                • EddyE Offline
                  EddyE Offline
                  Eddy
                  wrote on last edited by
                  #8

                  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.

                  Qt Certified Specialist
                  www.edalsolutions.be

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jk_mk
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • EddyE Offline
                      EddyE Offline
                      Eddy
                      wrote on last edited by
                      #10

                      What error do you get?

                      Qt Certified Specialist
                      www.edalsolutions.be

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jk_mk
                        wrote on last edited by
                        #11

                        error C2065: ‘ui->’ : undeclared identifier
                        Is difficult to understand the procedure. Could you be more spesiphic, or give me a simple example?

                        1 Reply Last reply
                        0
                        • EddyE Offline
                          EddyE Offline
                          Eddy
                          wrote on last edited by
                          #12

                          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?

                          Qt Certified Specialist
                          www.edalsolutions.be

                          1 Reply Last reply
                          0
                          • EddyE Offline
                            EddyE Offline
                            Eddy
                            wrote on last edited by
                            #13

                            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 started

                            You 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.

                            Qt Certified Specialist
                            www.edalsolutions.be

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved