1

I am using QFileSystemModel to populate my tree view. User can right click on a tree node (a folder) and perform an operation (exports all certain data files under this folder).

I have this export under the on_tree_clicked() but I feel like this operation belongs to model.

Will it be better if I derive my own QFileSystemModelWithExportfrom QFileSystemModeland added the export function? From on_tree_clicked(), I then just call it?

Is there another way to do this nicely? I want my on_tree_clicked() to be shorter and cleaner.

Also I am quite new to Qt, how do we derive from Qt core class like QFileSystemModel? When I am adding class, it lets me derive from QObject, QWidget etc but not from any model class.

zar
  • 11,361
  • 14
  • 96
  • 178

1 Answers1

2

Your UI should only deal with UI problems. i.e. button presses, user interaction.

This should then send it through to your model which actually executes the code.

Think of it this way, if your export to file takes, lets say 5 seconds, that's 5 seconds you can't use your UI for because that thread is doing the write to file. IF you have this model you have the opportunity to multi thread that particular event and keep your UI responsive whilst performing actions.

Code wise:

onTreeClicked() can be fairly short. It could perform the following:

myModel->writeDataFromNodeToFile();

& then inside that function you can have all the functionality you need to write to file. Open/Close/stream data etc

GPPK
  • 6,546
  • 4
  • 32
  • 57
  • This is what I was thinking too and it seems you are proposing deriving as from QFileSystemModel as well ..and how do I do that? – zar Feb 05 '15 at 16:17
  • http://stackoverflow.com/questions/10246984/how-to-override-re-implement-a-member-function-in-qfilesystemmodel – GPPK Feb 05 '15 at 16:20
  • It doesn't really say how to derive it..if you go add class, there is no option to pick QFileSystemModel as base class. – zar Feb 05 '15 at 16:59
  • @zadane You can type `QFileSystemModel` there. Doesn't have to be chosen from the combobox. – thuga Feb 06 '15 at 08:36
  • @thuga I did that and it works fine. I am coming VC++ world and it usually used to be added particularly way it addadd macros etc – zar Feb 06 '15 at 15:22
  • What would you suggest if I want to show a progress bar for this operation (assume its long)? I assume it will have to send messages to the window which will do updating..how to send this message in Qt? or should I simply pass pointer to the window? – zar Feb 10 '15 at 20:29
  • @zadane You should use signals and slots. Have a slot that updates the progress bar. Emit a signal that is connected to this slot that tells the progress bar to update. – thuga Feb 11 '15 at 13:18
  • @thuga good suggestion but I am not sure how, I have created this as another question here though http://stackoverflow.com/questions/28459518/how-to-show-progress-bar-from-a-function-in-model – zar Feb 11 '15 at 16:35