Qtableview editable cells
-
Hi, I have a weird situation.
I have a small dialog
When i click this push button, my code shows the following QTableview with the help of QSqlTableModel
I wanted this table view to be editable. To make it editable, my code has
void Case_Adjustment::on_pushButton_clicked() { setVisible(false); QSqlTableModel *model = new QSqlTableModel; model->setTable("adjusted_cases"); model->setEditStrategy(QSqlTableModel::OnRowChange); model->select(); QTableView *view = new QTableView; view->setModel(model); view->resize(1550,650); view->show(); }
But, the tableview is only editable when my previous dialog is either hidden or closed. THats fine, I hide() the (load case adjustment) dialog. But i am having difficulty in bringing it back after i am finished with editing.
I hope i am very clear with my problem. Can anybody help me in this regard? -
@Ahsan-Niaz said in Qtableview editable cells:
Can anybody help me in this regard?
It is not really clear what exactly you are doing. How do you show this dialog (is it modal?)? How do you show the table (own window?)? How do you try to show the dialog again.
-
void Case_Adjustment::on_pushButton_clicked() { setVisible(false); QSqlTableModel *model = new QSqlTableModel; model->setTable("adjusted_cases"); model->setEditStrategy(QSqlTableModel::OnRowChange); model->select(); QTableView *view = new QTableView; view->setModel(model); view->resize(1550,650); view->show(); }
@jsulm This code will help you understand everything. setVisible false hides the dialog which had push button. After editing the tableview, i want to show that hidden dialog again. Is it clear?
-
@Ahsan-Niaz Then you need to call show() on your dialog as soon as editing is done. I assume that "editing done" is when the user closes the table view (as you did not mention this). You could derive a custom class from QTableView and overwrite https://doc.qt.io/qt-5/qwidget.html#closeEvent where you can emit a signal. Connect that signal to show() slot of your dialog.
-
@Ahsan-Niaz
I don't know about your dialog (I see @jsulm has just replied).But I don't get how your
QTableView *view = new QTableView;
showsworks in practice. You have not set its parent, and you have not added it onto a layout So it's completely "standalone", not even inside a widget/window or with a layout? Plus it will "leak" as you don't retain any handle to it, and each time the button is pressed you'll get a new one. -
@JonB said in Qtableview editable cells:
So it's completely "standalone", not even inside a widget/window or with a layout?
Yes, it's a window as it does not have parent.
-
-
@JonB I have a dialog names (case_adjustment.ui). Initially, i added a tableview in it, and a push button to load the table. I loaded the table succesfully, but couldn't make the tableview editable.
Then someone here on QTFOrum suggested me the following codeQSqlTableModel *model = new QSqlTableModel; model->setTable("adjusted_cases"); model->setEditStrategy(QSqlTableModel::OnRowChange); model->select(); QTableView *view = new QTableView; view->setModel(model); view->resize(1550,650); view->show();
Now the problem is, i am able to edit things, and edits are saved as well, but i am unable to show the hidden dialog as i need it badly.
-
@Ahsan-Niaz
That was not the part I was talking about. Read @jsulm's earlier post about signals/slots for that part.My comments were to do with what you do from then on with the local
view
, not the editing behaviour. None of the code you show is to do with your separate dialog issue. -
@jsulm @JonB I am a newbie in qt creator. I have zero knowledge as compared to you guys.
I am literally unaware of deriving a custom class from QTableViewm, signal and slots. Can you help me a bit to understand and implement the concepts correctly.
@JonB I also want to ask how i can add TableView to a layout? and make it editable as well. Thanks for your replies i really appreciate. -
@Ahsan-Niaz Please read https://doc.qt.io/qt-5/signalsandslots.html - this is one of the core concepts in Qt you need to know.
Regarding deriving a class from another one: this is pure C++, not Qt specific. Same for overriding a method from base class. If unsure please read about OOP and inheritance. -
@jsulm I have OOP concepts, i have gone through the link you sent in your last reply. But, i am still finding it hard to derive a custom class from QTableview. As the Qtableview is available only in this piece of code
QTableView *view = new QTableView; view->setModel(model); view->resize(1550,650); view->show();
will this (view) object help me to derive a custom class? How?
-
@Ahsan-Niaz said in Qtableview editable cells:
will this (view) object help me to derive a custom class? How?
class MyView : public QTableView { ... signals: void closing(); protected: void closeEvent(QCloseEvent *event)override; } // In cpp void MyView::closeEvent(QCloseEvent *event) { emit closing(); QTableView::closeEvent(event); } ... MyView *view = new MyView; connect(view, &MyView::closing, this, &QDialog::show); view->setModel(model); view->resize(1550,650); view->show();
Don't forget that currently you have a memory leak: you're not deleting view.
-
Hi, Thanks for this piece of code
class MyView : public QTableView { ... signals: void closing(); protected: void closeEvent(QCloseEvent *event)override; }
I have added the above code in (.h) file, which looks like
Then i added the remaining codes in cpp file which looks like
When i run the code, i am getting an erro saying (Static Assertion Failed, No Q_Object in the class with the signal)
Here is the error
-
@Ahsan-Niaz Please put each class in its own header/cpp.
Also, if deriving from QObject based classes you need to add Q_OBJECT macro to your class (https://doc.qt.io/qt-5/qobject.html#Q_OBJECT):class MyView : public QTableView { Q_OBJECT ...
-
@Ahsan-Niaz
Additional to @jsulm. Separate problem. You have a member variableQSqlDatabase mydb
. Don't do this. I know there is a lot to learn, but as per https://doc.qt.io/qt-5/qsqldatabase.html#detailsWarning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.
You will end up falling foul of this, so follow the advice/example there and get it done now.
P.S.
When you have followed @jsulm's instruction to addQ_OBJECT
macro in header file, you may find compilation/linking goes confusingly wrong. If so, delete all files in the build directory and recompile from scratch. This can be required any time you start with a header file class which does not haveQ_OBJECT
, you compile, and then you decide to go back and add inQ_OBJECT
. -
@jsulm I have added a separate (.h) file named myview.h here is how it looks
I included (myview.h) in the cpp file which looks like this
I ran the code, it ran without error, but when i closed the tableview after editing, it didn't show the dialog. any expected reason?
Moreover, canyou explain thisconnect(view, &MyView::closing, this, &QDialog::show);
-
@Ahsan-Niaz Why is MyView::closeEvent in case_adjustment.cpp?!
As I said: put each class in its own header AND cpp file.
If you're done that and it still does not work, then please use debugger to find out whether MyView::closeEvent was called. -
@jsulm I really appreciate for your precious time and effort.
I have created a cpp file for myview, It looks like this
There is already a (myview.h) file.
I have run the code, it ran well, without error, but it didn't do the desired thing.
Have a look at case_adjustment.cpp as well
I debugged it as well,
But the debugger is not doing anything.
I have a request. If you can connect to my pc for a while? i have to submit it tonight, i would really appreciate it. Thanks -
Hi,
@Ahsan-Niaz said in Qtableview editable cells:
I have run the code, it ran well, without error, but it didn't do the desired thing.
What should happen ?
What does happen ?@Ahsan-Niaz said in Qtableview editable cells:
I have a request. If you can connect to my pc for a while? i have to submit it tonight, i would really appreciate it. Thanks
Sounds like a school assignment, doesn't it ?