Qt5 OpenFileDialog behaves strange on Gnome3 - c++

I am trying to get a file path at my Qt project. When I was using KDE4 and Qt 4.8 everything worked fine but now I updated to Qt 5 and Gnome 3. And the QFileDialog now looks like this:
Or even like this:
But more often it looks like this:
I noticed the behaviour changes depending on the path being passed as the initial path for the dialog. E. g., if I call
QString path = QFileDialog::getOpenFileName(qobject_cast<QWidget*>(this->parent()), tr("Choose model"), QCoreApplication::applicationDirPath(), QLatin1String("*.obj"));
then I get the second case screenshot.
Yet, if I call
QString path = QFileDialog::getOpenFileName(qobject_cast<QWidget*>(this->parent()), tr("Choose model"), QString(), QLatin1String("*.obj"));
then I get any folder empty while it is not (the first case screenshot).
Also, using this hint, I managed that commenting one line at my ~/.gtkrc-2.0 file and changing the theme to ambiance instead of adwaita at gnome-tweak-tool makes my application show the second-case screenshot.
I use this code to show up the dialog:
QString path = QFileDialog::getOpenFileName(qobject_cast<QWidget*>(this->parent()), tr("Choose model"), QCoreApplication::applicationDirPath(), QLatin1String("*.obj"));
Here, this points to the QGraphicsScene ancestor, OpenGLScene. Here is the code where I create one:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
GraphicsView view;
view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
view.setScene(new OpenGLScene);
view.show();
view.resize(1024, 768);
return app.exec();
}

Related

QApplication::setApplicationDisplayName not always falls back to QApplication::applicationName()

I am running a small app on KDE Plasma 5 created with Qt and the KDE framework. Almost everything works like a charm, just one part doesn't work. I just cannot set the application display name. I have the following code:
int main(int argc, char **argv) {
QApplication application(argc, argv);
KLocalizedString::setApplicationDomain("blender-render-control");
KCrash::initialize();
KAboutData aboutData(QStringLiteral("blender-render-control-center"),
i18n("Blender Render Control Center"),
QStringLiteral("1.0"),
i18n("A simple application to control the blender render control server"),
KAboutLicense::Custom,
i18n("Copyright 2019, Knerd <knerd#knerd.knerd>"));
aboutData.addAuthor(i18n("Knerd"), i18n("Author"), QStringLiteral("knerd#knerd.knerd"));
aboutData.setOrganizationDomain("knerd.knerd");
aboutData.setDesktopFileName(QStringLiteral("knerd.knerd.blender-render-control"));
KAboutData::setApplicationData(aboutData);
QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("knerd.knerd.blender-render-control")));
application.setApplicationDisplayName(i18n("Blender Render Control Center"));
application.setApplicationName(i18n("Blender Render Control Center"));
QCommandLineParser parser;
aboutData.setupCommandLine(&parser);
parser.process(application);
aboutData.processCommandLine(&parser);
auto *window = new MainWindow();
window->show();
return QApplication::exec();
}
From reading the docs and checking some examples, this should set the application title in my KDE environment. But it doesn't, the application name is the name of the executable.
Is this a bug in KDE or am I doing something wrong?

How to set an application icon in Qt

I have some trouble trying to set an icon for my QT application.
The icon is named "room.ico" and is on the same directory as the source file.
Here is the code :
#include <QApplication>
#include <QWidget>
int main( int argc, char *argv[ ] )
{
QApplication app( argc, argv) ;
QWidget fenetre;
fenetre.setWindowIcon(QIcon("room.ico")); // Nothing happens
fenetre.setWindowTitle("Heloo");
fenetre.show();
return app.exec() ;
}
I have tried to add win32:RC_ICONS += room.ico in the .pro file but that didn't work. I have also tried "./room.ico" but still no icon.
I have tried to use this :
QPixmap pixmap = QPixmap ("room.ico");
fenetre.setWindowIcon(QIcon(pixmap));
And guess what !!! it didn't work ... i'm just a newbie to QT :p
Any suggestions will be appreciated , thanks
QT's documentation for QWindow::setWindowIcon should be what you need.
Make an icon file (you appear to have done this already: room.ico
Add your icon file to a QT resource file (.qrc or .rc) which you should add to your project (the documentation discusses how to do this
Use setWindowIcon and pass in a QIcon:
app.setWindowIcon(QIcon(":/room.ico")); (this assumes your file is in the resource file)
Your problem appears to be that you didn't prepend :/ when passing in the filename to QIcon.

Qt5 QuickView cannot create window: no screens are available

I receive this error (title, below) whenever I try to run the following code:
#include <QCoreApplication>
#include <QQuickView>
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl::fromLocalFile("app.qml"));
QObject *object = (QObject*)view.rootObject();
view.show();
delete object;
return app.exec();
}
Cannot create window: no screens available
The program has unexpectedly finished.
All I can find online for that error are bug reports arising from specific conditions significantly more involved than the above.
app.qml is a file that runs fine alone, i.e. without the above C++ and in a separate project configured as a 'Qt Quick UI'. Giving it's qrc:// path, or deliberately specifying a file which does not exist has no effect.
Note the QObject* cast - this was not present in the docs, but without it:
/main.cpp:11: error: cannot initialize a variable of type 'QObject *' with an rvalue of type 'QQuickItem *'
How should this be done?
The QCoreApplication can be used with console application, not with GUI ones, i.e. you have to use a QGuiApplication object. It seems to me that you created a console application instead of a graphical one.
You can create a proper application via the Qt Quick Application, add your "app.qml" as a resource to that project and call such a file instead of the default "main.qml", provided by the project template.
If you want to quick fix your current project, just check that the .pro file is set to import GUI libraries:
QT += gui qml quick
Set your qml file as a resource:
Create a new resource file via file -> new File or Project... -> Qt -> Qt Resource File
Right click the newly created .qrc file and click add existing file to add your "app.qml" file
Finally, rewrite your main like this:
#include <QQuickView>
#include <QGuiApplication>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv); // GUI APPLICATION!!!
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:///app.qml")));
view.show();
return a.exec();
}
However, going for the Qt Quick Application project would be the wiser choice.

Qt 5.1 QApplication Without Display - QXcbConnection: Could not connect to display

I'm using Qt5.1 and I'm trying to create a QApplication without a display. I need to draw text with QPainter, so I need to use QApplication (or QGuiApplication), otherwise I get a segfault.
The application worked fine in Qt4.8, but fails in Qt5.1 on a headless version of Ubuntu with the error:
"QXcbConnection: Could not connect to display".
In Qt 4.8, I was able to use the following constructor with GUIenabled = false to create a QApplication that did not require a display:
QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )
In Qt5.1, the constructor for QApplication no longer has the GUIenabled flag.
I scanned the source code briefly, and there does seem to be a flag in the QApplication constructor, but it is undocumented as to what options can be used in that flag. Using "false" does not work.
How can I create a QApplication without a display? Is there an alternative method to telling QApplication GUIenabled = false? Alternatively, can I create a QCoreApplication that will not segfault when drawing text with QPainter on a QImage?
Yes, that's a Qt 3 (?) thing that is gone in Qt 5. Try running your application with the -platform offscreen command line option instead.
Note that you don't need QApplication or linking to QtWidgets to just draw upon a QImage, using QGuiApplication (and linking to QtGui) is sufficient.
If you want to create an app without GUI, you need to use QCoreApplication instead of QApplication.
Just hit this same issue. Really annoying that it at least isn't a compile error. My solution was just to use pointers and heap objects like,
QCoreApplication* app = 0;
Display* display = XOpenDisplay(NULL);
if (display)
{
XCloseDisplay(display);
app = new QApplication(argc, argv);
qobject_cast<QApplication*>(app)->setQuitOnLastWindowClosed(false);
}
else
{
app = new QCoreApplication(argc, argv);
}
return app->exec();

How to move a Qt stylesheet to an external file but keep it compiled in resources?

My Qt application has a large stylesheet with lots of margins, pixels and other stuff directly based on and related to drawing and graphics. I would be happy to give all that stuff to the designer, but the stylesheet is kept within the .ui file which is not convenient for the designer; she'd prefer to see a separate file and edit it using her usual tools.
What I want is moving the stylesheet to an external .qss file, adding that file to the program resources and linking it to the .ui file, so the stylesheet would be compiled and used by the widget automatically, and the application wouldn't have to keep the stylesheet file and load it at runtime.
How to achieve that?
Copy all your styles into a file and rename it to something like stylesheet.qss
Then include it in your qrc file as a new resource item. You can simply do this by editing your qrc file, if you already have one. Refer documentation on how to add a new resource.
Then modify your code like this to read the content of the qss file at run time and apply styles to your application.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile file(":/stylesheet.qss");
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
a.setStyleSheet(file.readAll());
file.close();
}
MainWindow w;
w.show();
return a.exec();
}
Note: when you do a change in the stylesheet.qss, you have to compile the qrc file for changes to take effect.
I set the stylesheet when the application is run from an external file. Also, for the designer we added a button to 'reload stylesheet'. This way the designer can modify the file and try the changes immediately.
For example:
QFile styleFile("stylesheet.qss");
styleFile.open(QFile::ReadOnly);
QByteArray bytes = styleFile.readAll();
QApplication *app = (QApplication*)QApplication::instance();
app->setStyleSheet(newStyleSheet);