Calling Classes with signal slots like a function



  • 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();
    
    }
    


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



  • @VRonin how and where





  • @VRonin not working




Log in to reply
 

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