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. Calling Classes with signal slots like a function
Qt 6.11 is out! See what's new in the release blog

Calling Classes with signal slots like a function

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.9k Views
  • 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.
  • S Offline
    S Offline
    sumit yadav
    wrote on last edited by VRonin
    #1

    Hello I am new to Qt creator and also to C++

    i have two classes. first is main class in which i have a ui with only a push buttons. second i have another class which add a label and and qlineedit. Now to to call the second class when the puchbutton is clicked. please find below the code

    this is mainwindow

     #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QLayout>
    #include <QFormLayout>
    #include <QButtonGroup>
    #include <QLineEdit>
    #include <QRect>
    #include <QStyle>
    #include <QScrollArea>
    #include <QtSql>
    #include <QDebug>
    #include <QtGui>
    #include <QtCore>
    #include <QDialog>
    #include <QLabel>
    #include <QMessageBox>
    
    #include "new_class.h"
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
        
    
    
    private:
        Ui::MainWindow *ui;
        QFormLayout* viewLayout;
        new_class* tab_1;
    };
    
    #endif // MAINWINDOW_H
    

    .cpp file

    #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()
    {
    
     // dont know how to call ???????
    }
    

    second call start from here

    
    #ifndef NEW_CLASS_H
    #define NEW_CLASS_H
    
    #include <QWidget>
    #include <QLineEdit>
    #include <QLayout>
    #include <QFormLayout>
    #include <QButtonGroup>
    #include <QRect>
    #include <QStyle>
    #include <QScrollArea>
    #include <QtSql>
    #include <QDebug>
    #include <QtGui>
    #include <QtCore>
    #include <QDialog>
    #include <QLabel>
    #include <QMessageBox>
    
    class new_class : public QWidget
    {
        Q_OBJECT
    public:
        explicit new_class(QWidget *parent = 0);
    
    signals:
    
    public slots:
    
    
    
    
    private:
        QLineEdit *new_line;
        QFormLayout* viewLayout;
    };
    
    #endif // NEW_CLASS_H
    

    second call .cpp file

    
    #include "new_class.h"
    
    new_class::new_class(QWidget *parent) : QWidget(parent)
    {
    
        viewLayout = new QFormLayout(this);
        QWidget* scrollAreaContent = new QWidget(this);
        scrollAreaContent->setLayout( viewLayout );
        QScrollArea* scrollArea = new QScrollArea(this);
        scrollArea->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
        scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
        scrollArea->setWidgetResizable( true );
        scrollArea->setWidget( scrollAreaContent );
        scrollArea->setGeometry(250,250,700,500);
        scrollArea->setEnabled(true);
        scrollArea->show();
    
        QLabel    *L  = new QLabel("ANYTHING THAT PASS TO IT", this);
        QLineEdit *Q  = new QLineEdit("Just to Check",this);
        L->setBuddy(Q);
        viewLayout->addWidget(L);
        viewLayout->addWidget(Q);
        L->show();
        Q->show();
    
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Just put a QWidget in your designer, right click on it and promote it to the type new_class. now you can access it through code as any other QWidget, something like ui->myNewClass->

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      S 1 Reply Last reply
      0
      • VRoninV VRonin

        Just put a QWidget in your designer, right click on it and promote it to the type new_class. now you can access it through code as any other QWidget, something like ui->myNewClass->

        S Offline
        S Offline
        sumit yadav
        wrote on last edited by
        #3

        @VRonin how and where

        VRoninV 1 Reply Last reply
        0
        • S sumit yadav

          @VRonin how and where

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @sumit-yadav said in Calling Classes with signal slots like a function:

          where

          @VRonin said in Calling Classes with signal slots like a function:

          in your designer

          i mean Qt Desinger to be clear


          @sumit-yadav said in Calling Classes with signal slots like a function:

          how

          @VRonin said in Calling Classes with signal slots like a function:

          right click on it and promote it

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          S 1 Reply Last reply
          1
          • VRoninV VRonin

            @sumit-yadav said in Calling Classes with signal slots like a function:

            where

            @VRonin said in Calling Classes with signal slots like a function:

            in your designer

            i mean Qt Desinger to be clear


            @sumit-yadav said in Calling Classes with signal slots like a function:

            how

            @VRonin said in Calling Classes with signal slots like a function:

            right click on it and promote it

            S Offline
            S Offline
            sumit yadav
            wrote on last edited by
            #5

            @VRonin not working

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              http://doc.qt.io/qt-5/designer-using-custom-widgets.html#promoting-widgets

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              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