Solved Change value of QLineEdit from other class
-
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.
-
Hi and welcome to devnet,
Why not just use a QSpinBox ?
Why are you creating FVenda in your delegate ?
-
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.
-
I would rather go with your model's dataChanged signal. That way you have more flexibility.
-
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);
-
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.
-
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.
-
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.
-
Thank you so much for the help, I pass the QLineEdit as a parameter in my constructor and work as I expected =D