QTreeWidget for Projects - c++

Well. I'm working on an IDE. Some of you maybe saw a post about it.
Well, i have no clue of how QTreeWidget & QtreeWidgetItem works since can't find a demo and the documentation doesn't help.
Well, what i'm trying to do is a IDE that you open the project file and then include all the files of the project to the tree. (Files in the project file are included by doing #include "filename"). How i do this?
Then you click a file and open it in a Tab (That was on other question). So in this part i just need an example of how to do the file click. :)

A simple solution would be to directly use QTreeWidgetItem. For every file in your project, create an instance of QTreeWidgetItem:
QTreeWidgetItem *file = new QTreeWidgetItem(browserWidget);
file->setText(0, filename);
Where "filename" is a string containing the name of your file. You can get the filename by parsing your project file looking for lines beginning with "#include".
By passing another QTreeWidgetItem in the constructor of a new item, you make the new item a child of the othe one. That way you can create directory structures.
To open a file, you can connect to the signal "itemDoubleClicked" of the QTreeWidget. You will get a pointer to the clicked widget item. Calling "text()" will retrieve the filename. If you have a directory structure, you need to do this too for all parent widget items. By concatenating the strings, you will get the path to your file.
You can find an example for this in the Qt Docs (see file settingstree.cpp)
As long as your IDE stays simple, this will be sufficient. A more flexible and "object-oriented" solution would be to create a subclass of QTreeWidgetItem. You will need to overwrite some methods. Since you probably only need read-only access the four methods described in the Qt Docs will be enough.

Related

Qt: How can I use my widgets created with code in the *.ui file

I have a project in Qt made with the QWizard and QWizardPage classes. There are two ways to create a widget i.e: a Label:
One is going to the *.ui file and search the element and put it where you want (visual way). Then you can access it on your code with ui->nameOfLabel.
The other one is going to your code and creating it like QLabel codedLabel;
Actually I'm using the second way (it's easier for me to create, show and use) but my question is: Is there any way that I can see my label codedLabel on the *.ui file?
I would like to move it to a space in the screen and in that case, it would be much easier for me to be able to do it through the visual way (but having the label created in the code instead of the ui).
Thank you so much.
Widgets created at runtime from your source code and being added to a widget as child CANNOT be seen in Qt Designer when you edit the .ui file of the widget they will be added to.
However, there could be an alternative (reading what you are trying to achieve: having some child widgets being present or not based on the context):
Create the widget from the .ui within Qt Designer and hide it (QWidget::hide()) or even remove it (QLayout::removeWidget()) programmatically if not needed at runtime.
If the real reason why you want to see it is because you want to "move it to a space in the screen and in that case, it would be much easier for me to be able to do it through the visual way". Then I recommend that you simply create an empty QWidget (or QLayout) in Qt Designer (graphically: easy to place where you want to) and later (programmatically) add your QLabel to it (rather than adding it to the main top-level widget): then, it goes in the place you determined from Qt Designer tool.
You should not need any complex code to programmatically display your QLabel in a specific place, just choose the right parent to have it be displayed in the right place!

How to create a browser for system drives, folders and files

I want to create as the following:
Unfortunately, Qt does not supported ready widget for that.
Is there is a plugin or any way to do that?
Use QFileSystemModel on a QTreeView. If you look at the first of those two links, it actually contains example code doing exactly that.
would personally suggest not use QWidgets for this task if you can avoid it. Instead, try to utilize the new shiny QML way of building Qt UI. It might be only my personal opition, but QTreeView has several flaws in my opinion.
QML
Here you can find a simple example how it is done with QML these days. It is using the FolderListModel from Qt.labs.folderlistmodel 2.1.
FolderListModel provides access to information about the contents of a folder in the local file system, exposing a list of files to views and other data components.
Note: This type is made available by importing the Qt.labs.folderlistmodel module. Elements in the Qt.labs module are not guaranteed to remain compatible in future versions.
import Qt.labs.folderlistmodel 2.1
The folder property specifies the folder to access. Information about the files and directories in the folder is supplied via the model's interface.
C++ and QWidgets
Should you insist on doing in C++ with the old QWidget set, your choice is probably to use QTreeView as it is a tree view after all and then combine that with QFileSystemModel.
The code would be something like this:
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
QTreeView *tree = new QTreeView(splitter);
tree->setModel(model);
tree->setRootIndex(model->index(QDir::currentPath()));

XCode assistant editor doesn't show my .h and my related .cpp file

If I create .cpp file from xcode, it will create .h file for me automatically, and I will be able to view two files together in Assistant Editor. It will show .h file relate to .cpp file right next to each other.
However, I create my project from TextMate and import those files manually. Now, when I click .h file it won't show the related cpp file right next to it. How can I make that happen.
Thanks
Hmmm.. I tried to recreate your problem, but it worked for me.
I have two thoughts:
1) Are you sure they are named the same thing? If not, it doesn't seem to consider them counterparts.
2) When you've switched to the Assistant Editor, to the right of the back/forward buttons is a dropdown menu to select which file to display. This should be set to "Counterparts" in order to get it to auto-display the correct counterpart.
If you have this set to manual, you can set it to maintain a certain file on the side.
Hope this helps!
Go into Xcode (v4) Organizer...Choose Projects.
Find your project and delete the Derived Data. Then rebuild the project.
Choose you view and in the Utilities View, in the Identity Inspector tab, In the Class text field, put down your class name.
Save
The relevant class will appear in the assistent editor.

GetOpenFileName Program-Defined Directory

I want to create a dialog box that resembles the one created with GetOpenFileDialog. However, I want the dialog to display a list of filenames that the program provides, and these filenames don't necessarily exist as files in a directory. The purpose is to provide a dialog with a similar look and feel for opening files, but rather inside of a zip file which the program would extract after selection. So, is there any way this is possible?
No, you'll have to write one yourself. You can however find the common dialog box templates in the PlatformSDK\Include directory; you can use the FileOpen.dlg template to build a dialog similar to the standard Open dialog.

Opening a file from a Qt URL

I'm coding a small and basic error tracker with Qt. The whole application is in a QTable.
Each error is linked to a file ; so, one of the columns of my table deals with that. I have a QLabel and a button next to it ; you click on the button to select a file, and then, the label displays the name of the file.
What I'd like to do now : the QLabel appears as a link, and when you click on it, it opens the file (with whatever app is associated to the file's extension). I'd rather it in the form of a link, because it's more obvious for the user. If I don't manage to do it, I'll go with a home QLabel herited class with a click signal, but it's not quite the same thing.
So, is what I want to do possible ?
And how would you do it ? Thanks in advance for your help !
You can use html in QLabel's text, so lets use that. Then set the QLabel to automatically open the link:
ui->label->setText("Link to file");
ui->label->setOpenExternalLinks(true);