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. Problems while using QPushButton
Forum Updated to NodeBB v4.3 + New Features

Problems while using QPushButton

Scheduled Pinned Locked Moved General and Desktop
10 Posts 7 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.
  • E Offline
    E Offline
    Eijin
    wrote on last edited by
    #1

    Hi everyone, i'm having some trouble using QPushButton, what i'm trying to do is to create a Button that, once clicked, makes my slider "value" 150. Here is the code i wrote, but, this is still not working :)

    @#include <QtGui/QApplication>
    #include <QSlider>
    #include <QPushButton>
    #include <QHBoxLayout>
    #include <QObject>

    int main(int argc, char *argv[])
    {

    QApplication app(argc, argv);
    
    QWidget *finestra= new QWidget;
    finestra->setWindowTitle("Player");
    
    
    
    QSlider *sli= new QSlider;
    sli->setRange(0,150);
    
    
    
    QPushButton *play= new QPushButton("Play");
    QObject::connect(play, SIGNAL(clicked()), sli, SLOT(setValue(150)));
    
    QHBoxLayout *lay= new QHBoxLayout;
    lay->addWidget(sli);
    lay->addWidget(play);
    lay->addWidget(te);
    finestra->setLayout(lay);
    finestra->show();
    
    
    
    return app.exec();
    

    }
    @

    Any idea?:) Thanks for the attention :)

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      It of course will not work. Try to read "about signals and slots in Assitant":http://doc.qt.nokia.com/4.7/signalsandslots.html . It will help you to understand better that mechanism.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

        You cannot assign arguments in a connect statement. Instead create a slot of your own and in that call sli->setValue( 150 ).

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alexisdm
          wrote on last edited by
          #4

          Or you can map the value 150 to the button with QButtonGroup, QSignalMapper or QStateMachine, if you want to stay a little while longer in the main function only.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #5

            Check the simple example of "QPushButton at our wiki":http://developer.qt.nokia.com/wiki/How_to_Use_QPushButton. Follow the code snippet and modify method handleButton() to change the value of your slider.

            http://anavi.org/

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rokemoon
              wrote on last edited by
              #6

              @
              #include <QtGui/QApplication>
              #include <QSlider>
              #include <QPushButton>
              #include <QHBoxLayout>
              #include <QObject>
              #include <QSignalMapper>

              int main(int argc, char *argv[])
              {
              QApplication app(argc, argv);

              QWidget *finestra= new QWidget;
              finestra->setWindowTitle("Player");
              
              QSlider *sli= new QSlider;
              sli->setRange(0,150);
              
              QPushButton *play= new QPushButton("Play");
              
              const int value = 150;
              QSignalMapper signalMapper;
              QObject::connect(play, SIGNAL(clicked()), &signalMapper, SLOT(map()));
              signalMapper.setMapping(play, value);
              
              QObject::connect(&signalMapper, SIGNAL(mapped(int)),
                            sli, SLOT(setValue(int)));
              
              QHBoxLayout *lay= new QHBoxLayout;
              lay->addWidget(sli);
              lay->addWidget(play);
              finestra->setLayout(lay);
              finestra->show();
              
              return app.exec();
              

              }
              @

              1 Reply Last reply
              0
              • L Offline
                L Offline
                loladiro
                wrote on last edited by
                #7

                For stuff like this I'm looking forward to lambda expressions with signal/slots in Qt5.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rokemoon
                  wrote on last edited by
                  #8

                  [quote author="loladiro" date="1313319918"]For stuff like this I'm looking forward to lambda expressions with signal/slots in Qt5.[/quote]
                  Hi loladiro, have you any examples of that?

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    loladiro
                    wrote on last edited by
                    #9

                    "This is currently being discussed to be merged into Qt5":http://developer.qt.nokia.com/wiki/New_Signal_Slot_Syntax
                    There's a link to the repository with the code in that article and there's also a merge request for it with qtbase.

                    And then stuff like this will be possible:
                    @
                    connect(sender, &Sender::valueChanged, [=](const QString &newValue) {
                    receiver->updateValue("senderValue", newValue);
                    } );
                    @
                    Or as is our example
                    @
                    QObject::connect(play, SIGNAL(clicked()), ={sli->setValue(150);});
                    @
                    THIS CODE IS NOT WORKING IN QT4, AND MIGHT NOT EVEN WORK IN QT5 AS THE SYNTAX IS SUBJECT TO CHANGE

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rokemoon
                      wrote on last edited by
                      #10

                      [quote author="loladiro" date="1313320494"]"This is currently being discussed to be merged into Qt5":http://developer.qt.nokia.com/wiki/New_Signal_Slot_Syntax
                      There's a link to the repository with the code in that article and there's also a merge request for it with qtbase.

                      And then stuff like this will be possible:
                      @
                      connect(sender, &Sender::valueChanged, [=](const QString &newValue) {
                      receiver->updateValue("senderValue", newValue);
                      } );
                      @
                      Or as is our example
                      @
                      QObject::connect(play, SIGNAL(clicked()), ={sli->setValue(150);});
                      @
                      THIS CODE IS NOT WORKING IN QT4, AND MIGHT NOT EVEN WORK IN QT5 AS THE SYNTAX IS SUBJECT TO CHANGE[/quote]
                      Thanks, really impressive.

                      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