How to copy a .ui file in my qwidgets project? - c++

I wish to try this qt example. I created a qwidgets project, copied all headers and sources from the following example to my project.
http://doc.qt.io/archives/qt-5.5/qtmultimedia-audiorecorder-example.html
What is the way to copy the .ui file of that project to my project?
What should be the extention of that file in my project?

The *.ui files are GUI designer files and define the actual GUI.
Copy it as-is. You'll need to invoke the uic (User Interface Compiler) on it to produce the ui_audiorecorder.h header file, which is included in the audiorecorder.cpp file.
Read more on the uic toolchain in the docs: Using a Designer UI File in Your Application.

You copy the .ui file into your project's folder using your favorite file manager or terminal or what have you. Suppose that the file is named foo.ui.
Then you add the file to the project by either:
Adding the following line to the .pro file:
FORMS += foo.ui
Right-clicking the root project node, selecting "Add Existing Files...", and navigating to the foo.ui file in the file dialog.
The two procedures are equivalent, the result is the same: a new FORMS item in the project file.
That's all. As soon as you save the .pro file, Qt Creator will parse it and show the foo.ui under a Forms project tree node.

Related

How to define a reusable folder path in Qt project file?

When working with a Qt project file *.pro one can use for example the $$PWD to reference to the folder containing the project file.
Is there a possible way to define a custom folder path?
Like saying: $$CUSTOMFOLDER = /home/user/folder
I would like to define CUSTOMFOLDER once and then reuse it in the project file so that I dont always have to paste the long folder path. By That the code would also become more readable.

VS2010 failing to locate source file

My project has following structure:
Someproject
include
workspace
bin
projectspace
source
I am using MFC for dialog based application. I add a new dialog and add a new class.Header and source files are generated in workspace/projectspace folder. I do exclude from project, move the files to respective location (includes/source) and do Add Existing Items in VS. Now when I add a button click event from dialog, it creates a new source file in includes directory. How to avoid this?
You should not do exclude. Instead, copy the files to the desired location, then remove from the project. Then Add existing from the new location.

Adding pdf as a resource in .qrc

I need to add a pdf file to my resource folder, which I can open from the application with the click of a button (kind of like a Help file).
So my confusions are:
How exactly do I add the pdf to my resources?
What path should I use to direct to the pdf?
QDesktopServices::openUrl(QUrl("/Resource Files/Help.pdf"));
To add the resource file into your project, right click on the project in the Projects pane and click Add New... and then Qt>Qt Resource file, enter a name for it and after finishing something is added to your .pro file like :
RESOURCES = myResource.qrc
Once added into the pro file, a separate .qrc file will be added into your Qt project with which you can double click and add your desired resources like icons, translation files etc. Open the resource file and add your pdf file there.
To open the pdf file from resources, you should first copy it to some location for example in the application directory path :
QFile HelpFile("qrc:/myFile.pdf");;
HelpFile.copy(qApp->applicationDirPath().append("/myFile.pdf"));
Next you can open the pdf file by :
QDesktopServices::openUrl(QUrl::fromLocalFile(qApp->applicationDirPath().append("/myFile.pdf")));

Genereted header file form ui file not visible?

I use QtCreator and QtDesigner to make a simple dialog. But when I tun Qmake I don't get a generated header file from the .ui file. I can use the member variables such as buttons and labels which I have made with QtDesigner. Is it normal this header to be invisible?
yes the file will be hidden
you can find the generated header in the build directory (a folder next to the project folder with) named ui_*.h this will also be where the moc_*.cpp files will be put

How to set application icon in a Qt-based project?

How do you set application icon for application made using Qt? Is there some easy way? It's a qmake-based project.
For Qt 5, this process is automated by qmake. Just add the following to the project file:
win32:RC_ICONS += your_icon.ico
The automated resource file generation also uses the values of the following qmake variables: VERSION, QMAKE_TARGET_COMPANY, QMAKE_TARGET_DESCRIPTION, QMAKE_TARGET_COPYRIGHT, QMAKE_TARGET_PRODUCT, RC_LANG, RC_CODEPAGE.
For Qt 4, you need to do it manually. On Windows, you need to create a .rc file and add it to your project (.pro). The RC file should look like this:
IDI_ICON1 ICON DISCARDABLE "path_to_you_icon.ico"
The .pro entry should also be win32 specific, e.g.:
win32:RC_FILE += MyApplication.rc
Verified in Linux (Qt 4.8.6) and Windows (Qt 5.6):
1) Add the icon file to your project folder;
2) In the main function call setWindowIcon() method. For example:
QApplication a(argc, argv);
a.setWindowIcon(QIcon("./images/icon.png"));
To extend Rob's answer, you can set an application icon for macOS by adding and modifying the following line onto your .pro file.
macx: ICON = <app_icon>.icns
Note that the ICON qmake variable is only meant to target macOS.
For Windows, use
RC_ICONS = <app_icon>.ico if you're attaching a .ico file
or RC_FILE = <app_icon>.rc if you want to attach your icon through a .rc file. (Be sure to add IDI_ICON1 ICON DISCARDABLE "myappico.ico" into the rc file. Indentation not mine.)
For further reading, see Setting the Application Icon.
For Qt5 on Windows, move your ico file to project folder and add this line to your .pro file:
RC_ICONS = myappico.ico
Offical link: https://doc.qt.io/qt-5/appicon.html
Now that Qt has upgraded to 5.0.1, there is new method to add an application icon. First, you need prepare a resource file, named as .qrc
1) Without Qt Designer, I assume there is a QMainWindow instance whose name is MainWin. You can use:
QIcon icon(":icon/app.icon");
MainWin.setWindowIcon(icon);
2) With Qt Designer, you can modify the property of QMainWindow. Choose icon resource from .qrc and insert it into the row of windowIcon.
The above method can be used in Qt4.7, Qt4.8.x.