Failed when using drawText() in main() function - c++

This is my code:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QRect r(0,8,32,16);
QImage img(32,32,QImage::Format_RGBA8888);
img.fill(Qt::Red);
QPainter p;
p.begin(&img);
QPen pen;
pen.setColor(QColor(0,0,0));
p.setPen(pen);
p.drawText(r,QString::number(i));
p.end();
QString str;
str.clear();
str.append("../../bmp/");
str.append(QString::number(2));
str.append(".bmp");
img.save(str);
return a.exec();
}
When gose to p.drawText(r,Qt::AlignCenter,QString::number(i));,Qt reports Error - RtlWerpReportException failed with status code :-1073741823.
But if I use other functions like drawRect or drawLine ,no faults are reported, why?

Font support must be part of GUI subsystem, both Qt framework-wise and platform-wise, assuming it's a XWindows application, Wayland or MS Windows.
Try replace QCoreApplication with QApplication and add GUI to configuration. Sadly, in case of Linux platform, you would require an active GUI server available.

Related

How to hide QMainWindow and show splashcreen during startup?

I am trying to hide the MainWindow of my Qt desktop app during startup, and to show a splashscreen. Both only happens after the loading phase, even though I call both splash.show() and window.hide() before the loading phase. I tried to split loading phase and constructor, but result is the same. How can I achieve both before the loading phase ?
Update 1
To display the splash screen, I had to add a call to QApplication::processEvents()
Update 2
The black window was actually not the MainWindow, but a ghost window that popped because scrollArea->setVisible(true) was called in the constructor.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(QStringLiteral(":/ressources/icons/icon.png"));
QSplashScreen splash(pixmap);
splash.show();
MainWindow window; // this loads for 5-6 seconds
a.processEvents();
window.showLoginPrompt();
splash.finish(&window);
return a.exec();
}
Based on your code and some example I could make it run like you are trying to do.
You only need to call your promptLogin function instead.
#include <QApplication>
#include <QTimer>
#include <QSplashScreen>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap("D:\\Projects\\SplashScreen\\TestSplashScreen\\splash.png"));
splash->show();
MainWindow mainWin;
QTimer::singleShot(2500, splash, SLOT(close()));
QTimer::singleShot(2500, &mainWin, SLOT(show()));
return app.exec();
}

settings windowtitle in maximized mode in qt

I want to know how to set window title in Qt in maximized mode.
window->setWindowtitle("window");
But this does not work in maximized mode.
playback_main *pbw = new playback_main;
pbw->setWindowTitle("PlayBack");
pbw->showMaximized();
This is not working.
Your code works for me on a QMainWindow in, admittedly, qt 4.7. Do you need to force an update with either of:
pbw->update();
qApp->processEvents();
is pbw a QMainWindow or just a widget?
I've tried the following code on linux which works too:
#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.setWindowTitle("testing");
w.showMaximized();
return a.exec();
}

How I show application when open application again Qt

Now, I have 1 application, but I don't want to open application twice, so I using QShareMemory to detect application when open twice.
And my question is: how I show current application in screen when user open application the second ?
int main(int argc, char *argv[]) {
Application a(argc, argv);
/*Make sure only one instance of application can run on host system at a time*/
QSharedMemory sharedMemory;
sharedMemory.setKey ("Application");
if (!sharedMemory.create(1))
{
qDebug() << "123123Exit already a process running";
return 0;
}
/**/
return a.exec();
}
Thanks.
Here's another approach in pure Qt way:
Use QLocalServer and QLocalSocket to check the existence of application and then use signal-slot mechanism to notify the existing one.
#include "widget.h"
#include <QApplication>
#include <QObject>
#include <QLocalSocket>
#include <QLocalServer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString appKey = "applicationKey";
QLocalSocket *socket = new QLocalSocket();
socket->connectToServer(appKey);
if (socket->isOpen()) {
socket->close();
socket->deleteLater();
return 0;
}
socket->deleteLater();
Widget w;
QLocalServer server;
QObject::connect(&server,
&QLocalServer::newConnection,
[&w] () {
/*Set the window on the top level.*/
w.setWindowFlags(w.windowFlags() |
Qt::WindowStaysOnTopHint);
w.showNormal();
w.setWindowFlags(w.windowFlags() &
~Qt::WindowStaysOnTopHint
);
w.showNormal();
w.activateWindow();
});
server.listen(appKey);
w.show();
return a.exec();
}
But if you're using Qt 5.3 on Windows, there's a bug for QWidget::setWindowFlags and Qt::WindowStaysOnTopHint, see https://bugreports.qt.io/browse/QTBUG-30359.
Just use QSingleApplication class instead of QApplication:
https://github.com/qtproject/qt-solutions/tree/master/qtsingleapplication
int main(int argc, char **argv)
{
QtSingleApplication app(argc, argv);
if (app.isRunning())
return 0;
MyMainWidget mmw;
app.setActivationWindow(&mmw);
mmw.show();
return app.exec();
}
It is part of Qt Solutions: https://github.com/qtproject/qt-solutions

QT can't debug a simple dialog program:Signal received.The inferior stopped because it received a signal

I fail to debug a simple dialog program ,it alerts:
"Signal received.The inferior stopped because it received a signal
from the Operating System.Signal name:SIGSEGV.Signal
meaning:Segmentation fault"
I use MinGW4.8 and QT SDK 4.8 on windows.
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
It alerts at :
w.show();
When UI shows,it alerts,and can't continue the debug.How can I debug succefully?

How to start a GUI application in the background?

How can I prevent a QApplication with a QMainWindow from stealing the keyboard focus when it starts? I'd like it to become active only after clicking on or alt-tabbing to it.
You could use showMinimized() instead of show() for you main window.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.showMinimized();
return app.exec();
}