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. Set Label Text from Another class

Set Label Text from Another class

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 4.9k 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.
  • R Offline
    R Offline
    ReDKiiL
    wrote on last edited by
    #1

    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
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ReDKiiL
        wrote on last edited by
        #3

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

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

          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.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            ReDKiiL
            wrote on last edited by
            #5

            Andre

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

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

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

              1 Reply Last reply
              0
              • R Offline
                R Offline
                roseicollis
                wrote on last edited by
                #7

                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()

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alex_malyu
                  wrote on last edited by
                  #8

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

                  @

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alex_malyu
                    wrote on last edited by
                    #9

                    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.

                    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