Set Label Text from Another class



  • I have a class/ui mainwindow.
    I would like to know how can I change a ui label mainwindow, through my sql class functions.

    Sorry my bad english.

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "qmessagebox.h"
    #include "sqlfunctions.h"
    sqlFuncs sqlFus;
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    this->setFixedSize(this->size());
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_btnLimpar_clicked()
    {
    ui->prodNome->clear();
    ui->prodMarca->clear();
    ui->prodQtd->clear();
    ui->prodPreco->clear();
    ui->plainTextEdit->clear();
    }

    void MainWindow::on_btnEnviar_clicked()
    {
    QString iprodNome = ui->prodNome->text();
    QString iprodMarca = ui->prodMarca->text();
    QString iprodQtd = ui->prodQtd->text();
    QString iprodDes = ui->plainTextEdit->toPlainText();
    QString iprodPreco = ui->prodPreco->text();
    int iqtd = iprodQtd.toInt(0,10);
    int ipreco = iprodPreco.toInt(0,10);
    int v = 0;
    if(ui->radioButton->isChecked()){
    v = 1;
    }else if(ui->radioButton_2->isChecked()){
    v = 2;
    }
    sqlFus.inserirProduto(iprodNome,iqtd,iprodMarca,iprodDes,ipreco,v);
    on_btnLimpar_clicked();
    }
    void MainWindow::setLabelText(QString ole){
    ui->statuslabel->setText(ole);
    }
    @

    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    void setLabelText(QString ole);
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_btnLimpar_clicked();

    void on_btnEnviar_clicked();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    sqlfunctions.cpp
    @#include "sqlfunctions.h"
    #include "QtSql"
    #include "QDebug"
    #include "mainwindow.h"
    void sqlFuncs::inserirProduto(QString n, int qtd, QString m, QString des, int pr, int v){
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("192.168.254.2");;
    db.setDatabaseName("lojinha");
    db.setUserName("root");
    db.setPassword("123456");
    bool ok = db.open();
    if(ok){
    QSqlQuery ins;
    ins.prepare("INSERT INTO produtos (nome,marca,descricao,qtd,preco,vender) VALUES(?,?,?,?,?,?)");
    ins.bindValue(0,n);
    ins.bindValue(1,m);
    ins.bindValue(2,des);
    ins.bindValue(3,qtd);
    ins.bindValue(4,pr);
    ins.bindValue(5,v);
    bool itswork = ins.exec();

        /* i try this */
        MainWindow::setLabelText("aaaaaaaaa");
    }else{
        QMessageBox::warning(0,"MySQL","Ocorreu um erro ao conectar ao servidor MySQL",QMessageBox::Ok);
    }
    

    }

    @
    sqlfunctions.h
    @#ifndef SQLFUNCTIONS_H
    #define SQLFUNCTIONS_H
    #include <QString>
    #include <QMessageBox>
    #include "mainwindow.h"

    class sqlFuncs{
    public:
    void inserirProduto(QString n,int qtd,QString m,QString des,int pr,int v);
    };

    #endif // SQLFUNCTIONS_H
    @



  • You can't call any function of the class (unless it is static and that is not the case ) without instantiating it first.

    First of all I would highly recommend against instantiating global instance of sqlFus.

    ( sqlFuncs sqlFus; )

    It would be much better to do it in main() or MainWindow.

    It is difficult to provide good advice, but normal C++ pattern
    would be to add public function to your MainWindow which sets text and pass
    and keep in sqlFuncs pointer to MainWindow instance,
    which it can use to call mentioned above function.



  • I Googled, and it seems that there is a way connect.
    Anyone know more about this?



  • You do not not want to directly access any private member of MainWindow from any other class or function. These are private for a reason, and that reason is encapsulation.

    However, you can of course offer a method set some value, which then results in changing something private. Still, that requires that you have a pointer or reference to the instance to change, not just the class as you are trying to do now.

    Qt offers a mechanism called signals and slots which you can connect. This functionality is offered by QObject. Please look that up in the documentation.



  • Andre

    You could talk more about pointers and references?
    Or any document that can explain this function? with some examples?
    Grateful.



  • No, that is basic C++. Just go to your local library and get a half-decent book on C++. Or do a little googling on the subject. We're here to discuss Qt, not the very basics of C++.



  • Hi, If what you want is to change the text of a label in Mainwindow, from another class, you need to create a slot in mainwindow where you will change the text and a signal in your other class and then use connect()



  • signal/slot discussion does not make sense, His class is not derived from QObject.
    simplified standard C++ pattern of communication would look like below

    @class B;
    class A {
    ...
    public:
    B* pB;

    }

    class B {
    public:
    ....
    void doSmth();
    }

    {

    B instanceB;
    A instanceA;

    instanceA.pB = &instanceB;
    }

    then within class A you can

    pB->doSmth();

    @



  • As you could see I pointed that it was "simplified pattern"
    My goal was to provide an idea of communication between classes for person who seems just started learning C++.
    You do not start learning complex ideas before you understand pointers.


Log in to reply
 

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