[Solved] Signals and Slots help needed



  • Hi All,
    I have written this code where i have created a from which takes input from a line edit and when a QPushButton is clicked, the slot buttonClicked() must be called which does some work. I initially used Qt Designer so it was very easy, where i had to right-click , "go to slot" and write the code there. Please help me out.

    widget.h

    @#ifndef WIDGET_H
    #define WIDGET_H

    #include <QtGui/QWidget>
    #include <QTabWidget>
    #include <QWidget>
    #include <QtGui>

    class Widget : public QWidget
    {
    Q_OBJECT

    public:
    Widget(QWidget *parent = 0);
    ~Widget();

    public slots:
    void buttonClicked();

    private:
    QLineEdit *enterjobid;
    QPushButton *viewbutton;
    QPushButton *clearbutton;
    QMessageBox *jobmessagebrowser;
    };@

    widget.cpp

    @#include "widget.h"

    Widget::Widget(QWidget *parent)
    : QWidget(parent)
    {
    QVBoxLayout *mainLayout = new QVBoxLayout;

    QHBoxLayout *row1 = new QHBoxLayout;
    QLabel *jobid = new QLabel(tr("Enter Job ID"));
    QLineEdit *enterjobid = new QLineEdit;
    row1->addWidget(jobid);
    row1->addWidget(enterjobid);
    
    QHBoxLayout *row2 = new QHBoxLayout;
    QPushButton *viewbutton = new QPushButton(tr("View Job Messages"));
    QPushButton *clearbutton = new QPushButton(tr("Clear"));
    row2->addWidget(viewbutton);
    row2->addWidget(clearbutton);
    
    QTextBrowser *jobmessagebrowser = new QTextBrowser;
    mainLayout->addLayout(row1);
    mainLayout->addLayout(row2);
    mainLayout->addWidget(jobmessagebrowser);
    setLayout(mainLayout);
    
    connect (clearbutton,SIGNAL(clicked()),
             enterjobid,SLOT(clear()));
    
    connect (viewbutton,SIGNAL(clicked()),
              this ,SLOT(buttonClicked()));
    

    }

    void Widget::buttonClicked()
    {
    connect (viewbutton,SIGNAL(clicked()),
    enterjobid,SLOT(selectAll()));
    // some work
    }@

    main.cpp

    @#include <QtGui/QApplication>
    #include "widget.h"

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

    return a.exec(&#41;;
    

    }@



  • So... what exactly is your problem?

    The code you have written for buttonClicked() might not do what you possibly expect (calling enterjobid->selectAll()), as the connection between viewbutton->clicked() and enterjobid->selectAll() ist not yet established at the time the clicked() signal is emitted (however, this will work the second time you click the button).

    If you just want to "call" a slot there is no need for a connection. Slots are ordinary functions which can be called through connections, but do not have to. So to select all in enterjobid just use
    @
    void Widget::buttonClicked()
    {
    enterjobid->selectAll();
    // some work
    }@



  • @
    void Widget::buttonClicked()
    {
    connect (viewbutton,SIGNAL(clicked()),
    enterjobid,SLOT(selectAll()));
    // some work
    }
    @

    Why do you do that???
    edit: Your description of what you want to do is a bit unclear too.



  • The problem is when i run the above code. The clear button works fine but when i click the viewbutton, the application output says - "The program has unexpectedly finished". I made above changes but got the same behavior.



  • Ok i generate the above form with a QLabel, QLineEdit, QPushbutton and a QTExtBrowser. When i will click "viewbutton" , i want to grab all the input from the lineedit, search for it and print a message in the textbrowser as "Job found" / "not found"

    Also i have a clear button which clears off contents of lineedit. It works fine using simple signal/slot mechanism.



  • Hi All,
    I solved it out. It was failing because of incorrect declarations. The final code is :

    widget.h

    @class Widget : public QWidget
    {
    Q_OBJECT

    public :
    Widget(QWidget *parent = 0);

    private slots:
    void buttonClicked();

    private:
    QLineEdit *enterjobid;
    QPushButton *viewbutton;
    QTextBrowser *jobmessagebrowser;
    };
    @

    widget.cpp

    @Widget::Widget(QWidget *parent)
    :QWidget(parent)
    {

    QVBoxLayout *mainLayout = new QVBoxLayout;
    
    QHBoxLayout *row1 = new QHBoxLayout;
    QLabel *jobid = new QLabel(tr("Enter Job ID"));
    enterjobid = new QLineEdit;
    row1->addWidget(jobid);
    row1->addWidget(enterjobid);
    
    QHBoxLayout *row2 = new QHBoxLayout;
    viewbutton = new QPushButton(tr("View Job Messages"));
    QPushButton *clearbutton = new QPushButton(tr("Clear"));
    row2->addWidget(viewbutton);
    row2->addWidget(clearbutton);
    
    jobmessagebrowser = new QTextBrowser;
    mainLayout->addLayout(row1);
    mainLayout->addLayout(row2);
    mainLayout->addWidget(jobmessagebrowser);
    setLayout(mainLayout);
    
    connect (clearbutton,SIGNAL(clicked()),
             enterjobid,SLOT(clear()));
    
    connect (viewbutton,SIGNAL(clicked()),
              this ,SLOT(buttonClicked()));
    

    }

    void Widget::buttonClicked()
    {
    connect (viewbutton,SIGNAL(clicked()),
    enterjobid,SLOT(selectAll()));

    //some work
    

    }
    @

    Hope this helps out someone.
    Thanks.


Log in to reply
 

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