I have a Qt Quick project and I just added some source files. When trying to build I get the error message:
QWidget: Cannot create a QWidget without QApplication
Since I have a Qt Quick project I use the QGuiApplication. QApplication is a subclass of QGuiApplication. How do I make QApplication available to the newly added sources? Or how do one solve it when one have a Qt Quick and a QWidget?
The source files are the QCustomPlot library which shows a graph.
EDIT:
main.cpp:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
//Register C++ classes with QML
qmlRegisterType<Bluetooth>("Bluetooth", 1, 0, "Bluetooth");
//Set start QML file
viewer.setMainQmlFile(QStringLiteral("qml/test/main.qml"));
//New Code:
// generate some data:
QWidget widget;
QCustomPlot * customPlot = new QCustomPlot(&widget);
QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x[i] = i/50.0 - 1; // x goes from -1 to 1
y[i] = x[i]*x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
customPlot->addGraph();
customPlot->graph(0)->setData(x, y);
// give the axes some labels:
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
customPlot->xAxis->setRange(-1, 1);
customPlot->yAxis->setRange(0, 1);
customPlot->replot();
//New Code End
//Show GUI
viewer.showExpanded();
return app.exec();
}
Error:
QML debugging is enabled. Only use this in a safe environment.
QWidget: Cannot create a QWidget without QApplication
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
The key concept is QWidget::createWindowContainer(). Try the following code:
#include <QQuickView>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(200, 200);
container->setMaximumSize(200, 200);
container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("qml/test/main.qml"));
...
}
You can find the details on the following posts:
Introducing QWidget::createWindowContainer()
Combining Qt Widgets and QML with QWidget::createWindowContainer()
Related
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.
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();
}
I have problem with my small app in Qt framework C++
I have a first window which there's two buttons where you can choose to play music or video. The music button will close the "choose window" and should open "music window" and similarly for video button.
I don't know how to do this... I know a way which I've leant and used but this method I'm going to explain how it doesn't fit to my current issue.
I've created a pointer of that window class in header of choose window and when the music button is clicked, I new the pointer and musicWindow->show(); and hide(); the choose window, this is good but there is a problem:
the new opened music window doesn't have any taskbar icon/thumbnail and when Its minimized there's no way to have that opened again(except wtih alt-tab)
and dont find a way to open it like a complete new window, I just can open all of them at once by using choosWindow.show(); / musicWindow.show(); /... .
I know there must be a way, but I dont even know what topics to search for to get further...
FirstWindow.cpp:
void FirstWindow::on_musicChoose_clicked()
{
//send a signal from here
}
void FirstWindow::on_videoChoose_clicked()
{
//send a signal from here
}
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FirstWindow w;
if(//what condition?)
{
MusicWindow mw;
mw.show();
}
if(//what condition?)
{
VideoWindow vw;
vw.show();
}
return a.exec();
}
I found my answer in Qt Forum:
https://forum.qt.io/topic/68602/child-window-in-taskbar/3
#Radek(Qt Forum): Try passing 0 (zero) as parent when you create them.
FirstWindow.cpp:
void FirstWindow::on_musicChoose_clicked()
{
this->hide();
mw = new MusicWindow(0); // passing nullptr as parent
mw.show();
}
void FirstWindow::on_videoChoose_clicked()
{
this->hide();
vw = new VideoWindow(0); // passing nullptr as parent
vw.show();
}
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FirstDialog w;
w.exec();
return a.exec();
}
I'm trying to display a Q3DScatter graph in a Main Window designed with Qt Designer so I've added a widget to the main window but I don't know how to embed the graph object in this widget. I tried to create a widget "container" programmatically and embed the graph in it and then putting the widget in a QHBoxLayout (which has been added to the main window using Qt Designer) using the following code in my main window's constructor:
...
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
But the window appears to be empty when executing. I'd really appreciate some help.
EDIT: Here's the full code of my main window constructor:
ResultsWindow::ResultsWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ResultsWindow)
{
ui->setupUi(this);
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
}
And here's the main code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ResultsWindow w;
w.show();
return a.exec();
}
EDIT 2: I forgot to specify that the horizontal layout is embedded in a GridLayout. I tried to create a new project and my code actually works perfectly when I add a horizontal layout directly to the main window. So could the problem be due to the GridLayout?
I am trying to print a QLabel from a combobox in QT. Code looks like this:
QApplication a(argc, argv);
QWidget w;
QVBoxLayout *layout = new QVBoxLayout(&w);
QLabel *label = new QLabel("Here you will see the selected text from ComboBox", &w);
QComboBox *combo = new QComboBox(&w);
layout->addWidget(label);
layout->addWidget(combo);
Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) {
combo->addItem(port.portName());
QObject::connect(combo, SIGNAL(currentIndexChanged(QString)), label, (SLOT(setText(QString))));
How do i print the label via cout?
Your code seems to be using Qt4, let's port that to Qt5 and a newer C++, shall we?
#include <QtWidgets>
#include <QtSerialPort>
int main(int argc, char ** argv) {
QApplication app(argc, argv);
QWidget w;
auto layout = new QVBoxLayout(&w);
auto label = new QLabel("Here you will see the selected text from ComboBox");
auto combo = new QComboBox;
layout->addWidget(label);
layout->addWidget(combo);
for (auto port : QSerialPortInfo::availablePorts())
combo->addItem(port.portName());
QObject::connect(combo, &QComboBox::currentTextChanged, [label, combo](){
label->setText(combo->currentText());
qDebug() << combo->currentText();
});
w.show();
return app.exec();
}
Try to not use Q_FOREACH in new code, it will probably be removed in the future,
Use auto when the type will be already specified by the new operator, this simplifies code,
Use qDebug to output debug information to the terminal,
Use lambdas in connections when the invoked code is short,
Use the new style connections for connections, because they will guarantee that your code actually works, the old style has runtime checks, and the new has build time checks.