Need help with PushButtons please!



  • Hello, I'm new to Qt and I can't figure out why my PushButtons are not connecting correctly. Here is my code:
    @#include "push_buttons.h"
    #include "ui_push_buttons.h"

    push_buttons::push_buttons(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::push_buttons)
    {
    ui->setupUi(this);
    ui->pushButton->setText("50% Progress");
    ui->pushButton_2->setText("Checked");
    ui->pushButton_3->setText("Fill");
    ui->pushButton_4->setText("Close");
    ui->progressBar->setValue(0);
    ui->checkBox->setChecked(false);

    connect(ui->pushButton,SIGNAL(clicked(bool)),
    ui->progressBar,SLOT(setValue(50)));

    connect(ui->pushButton_2,SIGNAL(clicked(bool)),
    ui->checkBox,SLOT(setCheckedState(bool)));

    }

    push_buttons::~push_buttons()
    {
    delete ui;
    }
    @



  • Hello, could you please provide more details, like tell me what you want your program to do?

    Thank You



  • Pardon. I'm simply practicing with push buttons and all I want it to do right now is set the progress bar to 50 when I click the first push button and to check the box widget when I click the second push button.



  • Two problems:

    1. You can't pass pre-defined values to slots when making a connection, unfortunately (many people would love to do that, but it's just not possible). So your SLOT (setValue(50)) is the problem.

    2. QCheckBox doesn't have a setCheckedState slot. You're probably looking for setChecked.



  • @
    #include <QApplication>
    #include <QPushButton>
    #include <QProgressBar>
    #include <QCheckBox>
    #include <QWidget>

    class myProgressBar : public QWidget
    {
    public:
    myProgressBar(QWidget *parent = 0);

    private slots:
    void setProgressBarValue();

    private:
    QProgressBar *bar;
    };

    myProgressBar::myProgressBar(QWidget *parent):QWidget(parent)
    {
    bar = new QProgressBar(this);
    bar->resize(200,25);
    bar->setOrientation(Qt::Horizontal);//Orientation can also be vertical
    bar->setRange(0,99);

    QPushButton *button = new QPushButton("Press Me", this);
    button->setGeometry(0,100,100,50);
    
    connect(button, SIGNAL(clicked()), bar, SLOT( setProgressValue() ));
    

    }

    void myProgressBar::setProgressBarValue()
    {
    int value = 50;
    bar->setValue(value);
    }

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

    myProgressBar progressBar;
    
    progressBar.resize(200,200);
    progressBar.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    I wrote that code trying to solve the question above, I now have a problem in my code, when I run that code it tells me "QObject::connect: No such slot QProgressBar::setProgressValue() in ../SimplePushButton/main.cpp:29". And the only way to set the progress bar is through the method setValue(). So any ideas how that problem could be solve, and please keep your answers simple.

    Thank You



  • @mahmoud899
    In your connect statement you specify bar (which is a QProgressBar object) as target and the slot as setProgressValue. QProgressBar has no such slot.
    I guess what you want is to call myProgressBar::setProgressBarValue when clicking on the button. Then you have to write

    @
    connect(button, SIGNAL(clicked()), this, SLOT(setProgressBarValue()));
    @

    The method setProgressBarValue() is a method of myProgressBar, therefore you have to use a myProgressBar object as target, and because you are in the myProgressBar constructor you use this as target.

    Peer



  • @Theagore

    There are 2 ways to achieve your goal:

    1. is to write the code from scratch
    2. is to use the Qt Designer.

    I have made what you asked for in the Qt Designer. Here is the code.

    mainwindow.h
    @

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp
    @

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    ui->progressBar->setValue(50);
    }

    void MainWindow::on_pushButton_2_clicked()
    {
    ui->checkBox->setChecked(true);
    }
    @

    main.cpp
    @

    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    Now you need to located the folder where you create the project in. Next you need locate the file named mainwindow.ui, open it with the text editor, and delete its contents and replace it with the below code

    @

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>MainWindow</class>
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>MainWindow</string>
    </property>
    <widget class="QWidget" name="centralWidget">
    <widget class="QProgressBar" name="progressBar">
    <property name="geometry">
    <rect>
    <x>10</x>
    <y>10</y>
    <width>271</width>
    <height>23</height>
    </rect>
    </property>
    <property name="value">
    <number>0</number>
    </property>
    </widget>
    <widget class="QPushButton" name="pushButton">
    <property name="geometry">
    <rect>
    <x>20</x>
    <y>50</y>
    <width>99</width>
    <height>27</height>
    </rect>
    </property>
    <property name="text">
    <string>Button1</string>
    </property>
    </widget>
    <widget class="QCheckBox" name="checkBox">
    <property name="geometry">
    <rect>
    <x>20</x>
    <y>90</y>
    <width>98</width>
    <height>22</height>
    </rect>
    </property>
    <property name="text">
    <string>CheckBox</string>
    </property>
    </widget>
    <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
    <rect>
    <x>20</x>
    <y>130</y>
    <width>99</width>
    <height>27</height>
    </rect>
    </property>
    <property name="text">
    <string>Button2</string>
    </property>
    </widget>
    </widget>
    <widget class="QMenuBar" name="menuBar">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>25</height>
    </rect>
    </property>
    </widget>
    <widget class="QToolBar" name="mainToolBar">
    <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
    </attribute>
    <attribute name="toolBarBreak">
    <bool>false</bool>
    </attribute>
    </widget>
    <widget class="QStatusBar" name="statusBar"/>
    </widget>
    <layoutdefault spacing="6" margin="11"/>
    <resources/>
    <connections/>
    </ui>
    @

    I hope this helps and solves your problem. When you like to connect the Widgets with each other in the Qt Designer, just click on the widget to you want to preform an action with and choose "Go to slot"



  • @PeerS

    I have changed the following line of code
    @
    connect(button, SIGNAL(clicked()), bar, SLOT( setProgressValue() ));
    @

    to this

    @
    connect(button, SIGNAL(clicked()), this, SLOT( setProgressValue() ));
    @

    and its still giving me the same problem "QObject::connect: No such slot QWidget::setProgressValue() in ../SimplePushButton/main.cpp:29". Any other suggestions?

    Thank You



  • Qt has a very, very good documentation. If you have a problem, say with QProgressBar or with how connect() works, look in the documentation and you will likely solve your problem very fast. For example, look at these pages:
    https://qt-project.org/doc/qt-4.8/qobject.html#connect
    http://qt-project.org/doc/qt-4.8/qprogressbar.html#value-prop

    If you study that, you will learn how to solve your current problems – you're just trying to use slots that don't exist on wrong objects.

    In QtCreator, you can put the cursor for example over "QProgressBar" and press F1, then QtAssistant will open with the documentation of QProgressBar showing, which is very handy for quick reference during your programming workflow.



  • @mahmoud899

    Look into your own code, the slot name is setProgressBarValue, not setProgressValue!!



  • mahmoud899 idea is correct. But the only correction required here is that you need to implement
    Custom Slot for the PushButton Clicked Signal. You can do this by writing your own .H and .CPP files in which you can make the UI class as member variable. The way how to write Custom slots is already explained in mahmoud899's MainWindow Class.

    The reason why you see that error is, you don't have that Slot by Default in QProgressBar Class



  • the compiler wouldn't know the slot's information, so when you use the connect function, make sure your connection is correct and no defined value to the arg. BTW, i m not good at english



  • Hello, I have figured out the problem, the solution for the problem is to separate everything, here is the following working code

    simple.h

    @
    #ifndef SIMPLE_H
    #define SIMPLE_H

    #include <QWidget>
    #include <QPushButton>
    #include <QProgressBar>

    class myProgressBar : public QWidget
    {
    Q_OBJECT

    public:
    myProgressBar(QWidget *parent = 0);

    private slots:
    void setProgressBarValue();

    private:
    QProgressBar *bar;
    };

    #endif // SIMPLE_H
    @

    simple.cpp

    @
    #include "simple.h"

    myProgressBar::myProgressBar(QWidget *parent):QWidget(parent)
    {
    bar = new QProgressBar(this);
    bar->resize(200,25);
    bar->setOrientation(Qt::Horizontal);//Orientation can also be vertical
    bar->setRange(0,99);

    QPushButton *button = new QPushButton("Press Me", this);
    button->setGeometry(0,100,100,50);
    
    connect(button, SIGNAL(clicked()), this, SLOT(setProgressBarValue()));
    

    }

    void myProgressBar::setProgressBarValue()
    {
    int value = 50;
    bar->setValue(value);
    }
    @

    main.cpp

    @
    #include <simple.h>
    #include <QApplication>

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

    myProgressBar progressBar;
    
    progressBar.resize(200,200);
    progressBar.show();
    
    return a.exec&#40;&#41;;
    

    }

    @



  • Looks good. May be you can create MainWindow in your main() and then make it parent for your myprogressBar instead of directly instantiating it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.