Multiple file edition
-
Well i'm trying to create a QTreeWidget with QTreeWidgetItem to store the files. Whenever someone clicks one of the items it opens the file in that specified item, read it's data and add a new tab and QPlainTextEdit to my QTabWidget and making that tab for THAT FILE ONLY. (Means that it stores the saved / un-saved contents of the file) How would i do that?
Then since we have multiple tabs to switch files, how would i assign each file to that tab? I meant, when someone changes tab it shows the text of the file assigned to that tab.
If you need better explain, tell me.
-
Well, what exactly is your problem?
Populate a QPlainTextEdit in the slot handling the QTreeWidget clicked signal and add it to the QTabWidget using QTabWidget::addTab(). Set the filename either as the tab label or as a dynamic property of the QPlainTextEdit so you can find out if a file is already opened by iterating over all opened files. Alternatively you can use an external mechanism of bookkeeping, like QHash<QString, QWidget*>, which keeps track of all opened files and the corresponding QPlainTextEdit widget, so you can easily switch to a specific file using QTabWidget::setCurrentWidget(), for example tabWidget->setCurrentWidget(openFilesHash.value(desiredFileName)).
-
[quote author="Forgive" date="1325168995"]QTabWidget doesn't have anything that let's us check if we're trying to change Tab.[/quote]
It has a "signal":http://developer.qt.nokia.com/doc/qt-4.8/qtabwidget.html#currentChanged that informs that we changed the tab.
-
[quote author="Forgive" date="1325205672"]I see. Also there's anyway to make the user able to left-click the Tab and show options? [/quote]
Which options are you talking about? You need to display a popup menu? I think the only way is to use an event handler that will catch the clicked event. Never done by myself.
-
Yes, sure that is possible. You can do such things, but you will need to handle the events yourself. Because you are using a QTabWidget, and that widget embeds QTabBar, subclassing isn't going to get you the events you need. You will need to use eventfilters. You can get a pointer to the embedded QTabBar using QObject::findChild<QTabBar*>. Then, you can install an event filter on it, and use any click the way you want it.
I have used this technique to add features to QTabBar and QTabWidget that allow renaming tabs by double clicking them, and to make it possible to prevent the switch of a tab (to allow validation).