[Solved] Saving List from QAbstractItemModel
-
I think there's a little misconception. I didn't mean that you include a button into the model. That would be completely against the sense of "model-view" programming. You would include something that belongs to the view (the button) into the model and thus would break the modularization.
The model should get a function that corresponds to an action (button press) in the user interface. In this case, a function that's called "save" for example and takes a QString for the filename where to save the contents of the model. This way, the model doesn't need to know anything about the user interface and whether a button caused the save function to be called or an automatic save algorithm triggered it. So we have preserved the decoupling of model and view.
On your main window, you just place a normal button and make it call the save function after asking the user for a filename via QFileDialog.
If you want your button aligned with the ListView displaying the strings, use a corresponding layout in QtDesigner.
If you want that button tighly integrated with the view, you'd need to subclass the view, too. But for the beginning I'd not recommend doing that (not because it's very hard, but because I don't see the benefit of adding a save-button permanently to a view, compared to just placing it with a layout). -
Thanks again.
I haven't been able to determine why my effort at subclassing QStringListModel doesn't work (described above), so my only hope is adding a button more directly. However, when I try that, by including the abstract model into a layout, I get an error.Here is my code:
@
QAbstractItemModel *model = new QStringListModel(textList);
QListView *view = new QListView;QPushButton *quitButton = new QPushButton("Quit"); QObject::connect(quitButton, SIGNAL(clicked()),qApp,SLOT(quit())); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(model); layout->addWidget(quitButton); QWidget window; window.setLayout(layout); window.show();
@
The error I get is
C2664: ‘QBoxLayout::addWidget’: cannot convert parameter 1 from ‘QAbstractItemModel*’ to ‘QWidget *’ -
The above error is coz you are trying to add a model to a layout
@layout->addWidget(model);@
you should try setting the model to QListView and adding it to the layout like:
@view->setModel(model)
layout->addWidget(view)@ -
Thank you!!! (I wondered if there was a way to tell it to treat it like a widget.)
The only problem now is that the view disappears immediately; its appearance is just a flash. This wasn't the case when I used
view->show();
to manifest the list.This problem isn't due to the list model; if I create a layout with just a button it, too, only flashes.
I'm once again scratching my head...
-
Is the above code written inside main.cpp. If yes then do check that you have added something like :-
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);// above code
return a.exec();
}@Otherwise you can share the code here so that we can easily check for the same.
If you are writing the above code inside a QMainWindow constructor
then you can directly use
@QFrame *frame = new QFrame(this);
setCentralWidget(frame);
frame->setLayout(layout);@ -
Hi and thanks again!
This code is not in the main (and the main looks like your example main). Instead it is in a method:void MainWindow::reviewSetup()
{QMessageBox questionBox; QWidget window; QStringList textList; QStringList changedTextList;
// make a list (next four lines)
textList=topScene->generateFileText();
foreach(DispDiagramScene *scene,dispSceneList) {
textList.append(scene->generateFileText());
}QAbstractItemModel *model = new QStringListModel(textList); QListView *view = new QListView; QPushButton *quitButton = new QPushButton("Quit"); QObject::connect(quitButton, SIGNAL(clicked()),qApp,SLOT(quit())); QVBoxLayout *layout = new QVBoxLayout; view->setModel(model); layout->addWidget(view); layout->addWidget(quitButton); window.setLayout(layout); window.show();
}
I'm not familiar with QFrame. Are you suggesting that I use the three lines you provided verbatim, instead of the final two lines in my existing code (the window code)?
Much obliged!
-
Well whatever you create on the stack is deleted when leaving the scope in which it was created. If you want anything to persist, create it on the heap (and you'll need to take care of memory management yourself). So in this case, you should be creating QWidget on the heap. (not QWidget window; but QWidget *window = new QWidget;)
(PS: use code tags around larger portions of code, see the "<>" button above the reply text box)
-
[quote author="BrawnyLad" date="1344959664"] Are you suggesting that I use the three lines you provided verbatim, instead of the final two lines in my existing code (the window code)?
[/quote]
Yes , try and check if it works.
-
[quote author="Sam" date="1344960186"]
[quote author="BrawnyLad" date="1344959664"] Are you suggesting that I use the three lines you provided verbatim, instead of the final two lines in my existing code (the window code)?[/quote]
Yes , try and check if it works.
[/quote]
No, it won't work as intended. First of all QFrame and QWidget don't make a functional difference here. Second he doesn't want to set the central widget of his main window, but create a new floating window, is that correct? -
[quote author="DerManu" date="1344960164"]Well whatever you create on the stack is deleted when leaving the scope in which it was created. If you want anything to persist, create it on the heap (and you'll need to take care of memory management yourself). So in this case, you should be creating QWidget on the heap. (not QWidget window; but QWidget *window = new QWidget;)
[/quote]
i was about to write the same thing. Thanks DerManu :) -
[quote author="DerManu" date="1344960274"]
[quote author="Sam" date="1344960186"]
[quote author="BrawnyLad" date="1344959664"] Are you suggesting that I use the three lines you provided verbatim, instead of the final two lines in my existing code (the window code)?[/quote]
Yes , try and check if it works.
[/quote]
No, it won't work as intended. First of all QFrame and QWidget don't make a functional difference here. Second he doesn't want to set the central widget of his main window, but create a new floating window, is that correct?
[/quote]Yes QFrame and QWidget doesnt make much difference here. The main thing is to create on heap instead of stack. If it is a floating widget then widget->show() can be used otherwise setCentralWidget() in order to add it to MainWindow.
Edit: For a sample application i was using QFrame so pasted the same code. use QWidget :)
-
Thank you DerManu and Sam!
With your help I was able to succeed!
When you say "You'll need to take care of memory management yourself", that means I'll have to delete the QWidget when I am done with it, right? In Qt I can do that with a simple command, (rather than by subclassing QWidget to create a destructor)?
-
Can I do it as simply as:
window->setAttribute(Qt::WA_DeleteOnClose);
? -
Yes, if that is your requirement. I.e. it won't be shown again once it is closed.
-
Thank you again, DerManu. (I'll close this thread now.)