Qt example for displaying text from line edit to label on button click using signal and slots

//connectButtonClose.pro
#-------------------------------------------------
# Project created by QtCreator
#-------------------------------------------------
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = connectButtonClose
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
//---------------
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
   // MainWindow w;
   // w.show();
MainWindow *w = new MainWindow;
w->setUI();
    return a.exec();
}
//-------------
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>
#include <QLineEdit>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<endl;
    ui->setupUi(this);

//    qpb =new QPushButton("HitMe");
//    hl = new QHBoxLayout;
//    le = new QLineEdit();
//    label1 =new QLabel;
//    hl->addWidget(le);
//    hl->addWidget(qpb);
//    hl->addWidget(label1);

//    myWidget = new QWidget;
//    myWidget->setLayout(hl);
//    myWidget->show();
//    QObject::connect(qpb,SIGNAL(clicked()),this,SLOT(slotCalled()));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete qpb;
    delete hl;
    delete label1;
}
void MainWindow::setUI()
{
    qpb =new QPushButton("HitMe");
    hl = new QHBoxLayout;
    le = new QLineEdit();
    label1 =new QLabel;
    label2 =new QLabel;
    hl->addWidget(le);
    hl->addWidget(qpb);
    hl->addWidget(label1);
    hl->addWidget(label1);

    myWidget = new QWidget;
    myWidget->setLayout(hl);
    myWidget->show();

    QObject::connect(qpb,SIGNAL(clicked()),this,SLOT(slotCalled()));
    //QObject::connect(qpb,SIGNAL(clicked()),this,SLOT(slotCalled2()));
   // QObject::connect(qpb,SIGNAL(mysignal()),this,SLOT(slotCalled()));
    QObject::connect(this,SIGNAL(mysignal()),this,SLOT(slotCalled2()));
    QObject::connect(this,SIGNAL(mysignal()),this,SLOT(slotCalled3()));//one signal connected with 2 slots gets called in order of connect
}

//void MainWindow::mysignal()
//{
//    qDebug()<<"Signal is called.\n\n\n";
//}
void MainWindow::slotCalled()
{
    qDebug()<<"slotCalled"<<endl;
    //QString data = le->text();
   // qDebug()<<"data"<<data<<endl;
    //label1->setText(data);
    //label1->setText(le->text());
    qDebug()<<"emmitting mysignal from slotCalled()..."<<endl;
    emit mysignal();

}
void MainWindow::slotCalled2()
{
    qDebug()<<"slotCalled2"<<endl;
//    QString data = le->text();
//    qDebug()<<"data"<<data<<endl;
//    label1->setText(data);
    label1->setText(le->text());

}
void MainWindow::slotCalled3()
{
    qDebug()<<"slotCalled3"<<endl;
}

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QLabel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    QLineEdit *le;
    Ui::MainWindow *ui;
    QPushButton *qpb ;
    QHBoxLayout *hl;
    QWidget *myWidget;
    QLabel *label1;
    QLabel *label2;
public slots:
    void slotCalled();
    void slotCalled2();
    void slotCalled3();
signals:
//    Q_SIGNALS:
    void mysignal();
public:
    void setUI();
};

#endif // MAINWINDOW_H
//mainwindow.ui
<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="QMenuBar" name="menuBar" />
  <widget class="QToolBar" name="mainToolBar" />
  <widget class="QWidget" name="centralWidget" />
  <widget class="QStatusBar" name="statusBar" />
 </widget>
 <layoutDefault spacing="6" margin="11" />
 <pixmapfunction></pixmapfunction>
 <resources/>
 <connections/>
</ui>
//Steps to build: download the source code in a folder. Navigate to the folder. qmake connectButtonClose.pro. then make.

Frequently asked questions in Qt interviews

No comments:

Post a Comment