Referring to this question.
After embedding an external application with the code:
#include <QApplication>
#include<QProcess>
#include<QDesktopServices>
#include<QUrl>
#include<QWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *w = new QMainWindow();
w->setWindowTitle("Genome Embedded Calculator");
QWindow* window = QWindow::fromWinId(41943047); // Hardcoded Window Id for Genome Calculator
window->setFlags(Qt::FramelessWindowHint);
QWidget *widget = QWidget::createWindowContainer(window);
w->setCentralWidget(widget);
w->show();
return a.exec();
}
How to detach the QWindow after closing the application?
I want the gnome-calculator process to continue working after closing my application.
Related
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();
}
How I can change title shown on the picture to "№". Thanks.
That widget is an object of the QTableCornerButton class that inherits from QAbstractButton but it is a class that is part of the private Qt API that does not use text, so you can not use setText() of QAbstractButton, so the other option is establish a QLabel with a layout above:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableView w;
QStandardItemModel model(10, 10);
w.setModel(&model);
QAbstractButton *button = w.findChild<QAbstractButton *>();
if(button){
QVBoxLayout *lay = new QVBoxLayout(button);
lay->setContentsMargins(0, 0, 0, 0);
QLabel *label = new QLabel("№");
label->setContentsMargins(0, 0, 0, 0);
lay->addWidget(label);
}
w.show();
return a.exec();
}
I am using the Urho3D engine with Qt for an application. The problem is that both Urho3D and QApplication require to be ran from main(). For now I am using it in separate processes but IPC makes it complicated.
Is there any way to solve this issue? Thanks
My platform is Urho3D 1.5, Qt 4.71 and Windows 7 x64 and VS2015 (C++)
I'm new to both c++ and Urho3D, but I've successfully achieved it.
Simple code, haven't had further test:
awidget.h:
#ifndef AWIDGET_H
#define AWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <Urho3D/Engine/Application.h>
class aWidget : public QWidget
{
Q_OBJECT
public:
explicit aWidget(QWidget *parent = 0)
{
QPushButton *button = new QPushButton(this);
connect(button, SIGNAL(clicked()), this, SLOT(pressed()));
}
public slots:
void pressed()
{
Urho3D::Context* context = new Urho3D::Context();
Urho3D::Application *application = new Urho3D::Application(context);
application->Run();
}
};
#endif // AWIDGET_H
main.cpp:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
aWidget *widget = new aWidget();
widget->show();
return app.exec();
}
By the way, I'm using Qt 5.9.0
So the answer is quite simple. Instead of running QApplication by calling
app->exec();
it is needed to manually and regularily call this from your main loop:
app->processEvents();
This will take care that all events used by Qt are processed and QApplication will respond accordingly.
Example:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
bool shallrun = true;
aWidget *widget = new aWidget();
widget->show();
while (shallrun)
{
app->processEvents();
...
}
...
}
With the following code I generate a window with buttons:
#include <QApplication>
#include <QtWidgets>
class Item : public QWidget {
QHBoxLayout hLayout{this};
QPushButton bt{"button"};
public:
Item() : QWidget() {
hLayout.addWidget(&bt);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget w;
QVBoxLayout vLayout(&w);
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
w.show();
return app.exec();
}
With layout.setContentsMargins(0,0,0,0) I can go to:
Is it possible to reduce even more space between buttons?
You can try using setSpacing method :
vLayout.setSpacing(0);
But I think the real issue here would be that your layout is stretched out to cover whole widget and it arranges layout items accordingly.
Another thing you can try is to set the margin :
vLayout.setMargin(0);
You can remove the space, by adjusting the spacing:
http://doc.qt.io/qt-5/qlayout.html#spacing-prop
Setting it to 0 should bring the widgets together.
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget w;
QVBoxLayout vLayout(&w);
vLayout.addWidget(new Item());
...
// Removes outer margins
vLayout.setContentsMargins(0,0,0,0);
// Set space between items to '0'
vLayout.setSpacing(0);
w.show();
return app.exec();
}
In your example above you should probably also add a strech section:
http://doc.qt.io/qt-5/qboxlayout.html#addStretch
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();
}