Changing Default Source File On Qt Creator - c++

I'm simply looking for a quick and easy solution on how to change the default source code (if at all possible) for when you open a qtCreator C++ project.
As soon as you open up a new project you see something like below in your main.cpp file.
#include <QCoreApplication>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
return a.exec(); }
I'm wondering if there's a way to change this code snippet to user defined code. I personally just think its a hassle to wipe all this code out every time I create a project and would rather it look something like below, when I make a new project.
#include <QCoreApplication>
#include <iostream>
using namespace std;
int main() {
}
Anyways, open to all different types of solutions just let me know...

Related

How to specify the way a code snippet is marked up?

Goal
For documentation purposes I would like to export a C++ code snippet from QtCreator as an HTML file and then use CSS to highlight its syntax, for example like that:
Since QtCreator does not have an export option, I am using QDoc to achieve that.
As per documentation, I use \quotefile in a qdoc-file in my project to turn the source code from main.cpp into an HTML:
/*!
\page index
\quotefile main.cpp
*/
Running qdoc produces an index.html and when I open it, there is indeed the marked-up content of main.cpp.
Note: To demonstrate the issue I am using the code from the documentation:
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton hello("Hello world!");
hello.resize(100, 30);
hello.show();
return app.exec();
}
Problem
When I inspect the produced html code in the browser, for the hello( part for example, I get:
hello(
When I inspect the html code of the documentation however, which I believe is generated with the help of QDoc as well, for the same part I see:
<span class="pln"> hello</span>
I do not know why in my case the additional markup is missing.
Question
How to setup QDoc so, that I can specify the rules of how each part of the code snippet should be marked up?

QSettings not saving ini changes without admin privileges

Problem
I am currently learning Qt by writing a mp3 player on Windows. Now I want to use QSettings to save changes made to the application. I already tried saving the changes to the registry (which works fine) but I would like to store them in an ini-file (for portability reasons).
But for some reason I am unable to create or write an existing ini-file (reading works perfectly).
What I've tried so far
run the application with admin privileges, which works but is not viable all the time
use QStandardPath to ensure a writable path
MCVE
#include <QCoreApplication>
#include <QSettings>
#include <QDebug>
#include <QFileInfo>
#include <QStandardPaths>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QString path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
path = path + "/settings.ini";
QFileInfo info(path);
qDebug() << info.exists();
qDebug() << info.isWritable();
QSettings settings(path, QSettings::IniFormat);
settings.setValue("key", "value");
settings.sync();
return 0;
}
Question
Why does the ini-file only get generated / updated when the application is running with admin privileges? Is there any way to do it without?
It's not clear what versions of OS are used. Windows 10 (version 1709) come with new feature called "Protected folders" to prevent e.g. Ransomware threats. You can reconfigure it somewhere. It disallow write access to desktop/documents etc.
But more preferable approach will be to store ini file into other location e.g. AppData.

In Qt main function, how does QApplication learn about Mainwindow?

Looking at a simplest Qt Widget sample application that you can find from almost every Qt tutorial:
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();
return a.exec();
}
There is one thing puzzles me. There are two major variables a and w here. a.exec() starts Qt's main loop, which suppose to interact with the main GUI component w. However, both of them live on stack and I don't see any code pass w somehow to a. So how does a be aware of the existence of w?
Does the constructor of w initializes a static data structure that a can access to check the top-level widgets?
Qt preprocess your code and build the real c++ code before compiling, its at this moment QApplication wrap all Q object in the main.cpp file and build the rest of the code from it.

Reading a CSS file using Qt C++

I'm trying to load in a CSS file for formatting across my entire Qt application. Currently I have my "stylesheet.css" file in the same folder as my built exe (both debug and release). However, upon running the program it produces no errors and simply outputs "test: ", so it's clearly not finding the file or perhaps I'm not reading it properly?
Forgive me if it's a dumb mistake - I'm fairly new to both Qt and C++.
#include "mainwindow.h"
#include "qfile.h"
#include "qtextstream.h"
#include <QApplication>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication program(argc, argv);
QFile styleFile("stylesheet.css");
styleFile.open(QIODevice::ReadOnly);
QTextStream textStream(&styleFile);
QString styleSheet = textStream.readAll();
styleFile.close();
program.setStyleSheet(styleSheet);
std::cout << "test: " << styleSheet.toStdString() << std::endl;
MainWindow w;
w.showMaximized();
return program.exec();
}
After digging a bit deeper, it turns out I was lacking a QRC file (which I wasn't even aware existed). So I created a resource (QRC) file, added the prefix "/style", and then added my stylesheet to that prefix. It now works flawlessly. Also, at one point I changed the .css to a .qss (thought I should mention it, although I doubt it made any difference).
Here's the final code:
#include "mainwindow.h"
#include "qfile.h"
#include "qtextstream.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication program(argc, argv);
QFile styleFile(":/style/stylesheet.qss");
if(styleFile.open(QIODevice::ReadOnly))
{
QTextStream textStream(&styleFile);
QString styleSheet = textStream.readAll();
styleFile.close();
program.setStyleSheet(styleSheet);
}
MainWindow w;
w.showMaximized();
return program.exec();
}
Change
QFile styleFile("stylesheet.css");
to
QFile styleFile("C:/path/to/stylesheet.css");
You need to pass the full file path for your program to find the file. If you just give its name but not the directory it's in, the program will search for it in the current directory only (which is not necessarily the directory of your exe file), and if it's not there, it won't find it.
If you want to deploy this CSS file with your application, you are looking for the Qt resource file support.
its coz u shoud uncheck " shadow build " under projects. and check the path of the project . and make sure that .css file is inside the project folder

Qt custom look and feel?

can I force my Qt application to use different look and feel just like it is done in KDE ?
You can always change styles of widgets using QApplication::setStyle.
There are a few predefined options available in Qt4.
In main.cpp do something like this
#include <QPlastiqueStyle>
int main(int argc, char *argv[])
{
[...]
QApplication::setStyle(new QPlastiqueStyle());
}
This way your application will alwyas look the same on different OS.
In my opinion Plastique looks better under windowsXP/2000 then default QWindowsXPStyle.
Cleanlooks is quite nice too.
There are other options:
#include <QPlastiqueStyle>
#include <QCleanlooksStyle>
#include <QWindowsXPStyle>
#include <QWindowsVistaStyle>
#include <QMotifStyle>
#include <QCDEStyle>
I hope this helps.
You can use CSS to style widgets https://doc.qt.io/qt-5/stylesheet.html