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. [Solved] Signals and Slots help needed
Forum Updated to NodeBB v4.3 + New Features

[Solved] Signals and Slots help needed

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.3k 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.
  • A Offline
    A Offline
    amban
    wrote on last edited by
    #1

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

    }@

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      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
      }@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aureshinite
        wrote on last edited by
        #3

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

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

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            amban
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              amban
              wrote on last edited by
              #6

              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.

              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