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. Change value of QLineEdit from other class
Forum Updated to NodeBB v4.3 + New Features

Change value of QLineEdit from other class

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 787 Views 2 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
    Ritch
    wrote on last edited by
    #1

    Hello, I'm trying to change a value of a QlineEdit from another class, which I used to insert a format in a specific cell in my QTableView, I connected the LineEdit signal that I used to formatted the cell with the slot in my Venda class, which is responsible for changing the value of QLineEdit, everything goes well, until you update the data in QLineEdit, which doesn't happen and I don't know what to do to update this value.

    Only_Number_Class.h

    #include <QStyledItemDelegate>
    
    class Only_Number : public QStyledItemDelegate
    {
    Q_OBJECT
    public:
        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    };
    
    #endif // ONLY_NUMBER_CLASS_H
    

    Only_Number.cpp

    QWidget *Only_Number::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QLineEdit *lineEdit = new QLineEdit(parent);
        // Set validator
        QIntValidator *validator = new QIntValidator(lineEdit);
        lineEdit->setValidator(validator);
    
        Venda *FVenda = new Venda();
    
        connect(lineEdit, &QLineEdit::editingFinished, FVenda, &Venda::Total_Compra);
    
        return lineEdit;
    }
    

    Venda.h

    namespace Ui {
    class Venda;
    }
    
    class Venda : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Venda(QWidget *parent = nullptr);
        ~Venda();
    
    public slots:     
        void Total_Compra();
    
    private slots:
        void on_Btt_Buscar_clicked();
    
        void on_Chk_Nome_stateChanged(int arg1);
    
        void on_Chk_Categoria_stateChanged(int arg1);
    
        void on_Tbv_Carrinho_doubleClicked(const QModelIndex &index);
    
        void on_Tbv_Produto_doubleClicked(const QModelIndex &index);
    
        void on_Tbv_Carrinho_activated(const QModelIndex &index);
    
        void on_Tbv_Carrinho_pressed(const QModelIndex &index);
    
        void on_Tbv_Carrinho_clicked(const QModelIndex &index);
    
    private:
        Ui::Venda *ui;
    };
    
    #endif // VENDA_H
    

    Total_Compra in Venda.cpp

    void Venda::Total_Compra(){
        double Total_Compra = 0;
    
        for (int row = 0; row < Model_Car->rowCount(); row++){
            Total_Compra += Model_Car->item(row, 3)->text().toDouble() * Model_Car->item(row, 4)->text().toDouble();
        }
    
        QString Formated_Num = QString::number(Total_Compra, 'f', 2);
         
        ui->Txt_Total_Compra->setText(Formated_Num);
    }
    

    Thanks in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Why not just use a QSpinBox ?

      Why are you creating FVenda in your delegate ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Hello,
        About QSpinBox, you are right, i started with the QLineEdit because I see this delegate in a example and I forgot to change, thanks for remember me.

        About FVenda, I need a way to verify when a specifie collumn of QTableView is finished of editing, the way which I found to do this, was connect the signal of QLineEdit to a Public Slot that I made, to sum the values and update the total value in QLineEdit (Txt_Total_Compra). I created the FVenda to connect the signal of my Only_Number (EditingFinished of QLineEdit)to slot of my Venda.

        Thanks for replying.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I would rather go with your model's dataChanged signal. That way you have more flexibility.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • R Offline
            R Offline
            Ritch
            wrote on last edited by Ritch
            #5

            Ok, I will change that. But the signal EditingFinished is work fine, my problem is when I call the Total_Compra function slot, after finished my editing, the value of Formated_Num, which I already verify through qDebug and it have my correct information, is not updated in my Txt_Total_Compra and I don't know why.

            this line dont work.

            ui->Txt_Total_Compra->setText(Formated_Num);
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Venda in your delegate is a dialog that you never show.

              You are likely creating your delegate in your Venda class, correct ? So in fact, it's working but within the instance you created in your delegate and not the one you actually have before your eyes because these two are completely different objects.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                Ahhh I understand now the problem, thank you, then there is a way to my signal of QLineEdit in my delegate class access the Object which I want? I already tryed to use the CloseEditor signal of QAbstractItemDelegate, but don't work for me, the better result which I found until now is this.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You can pass your widget as parameter to your delegate constructor and store it as member variable. Then do the connection in the createEditor method.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • R Offline
                    R Offline
                    Ritch
                    wrote on last edited by
                    #9

                    Thank you so much for the help, I pass the QLineEdit as a parameter in my constructor and work as I expected =D

                    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