I have created a Qt GUI application but I haven't touched anything regarding the GUI. I have modified mainwindow.cpp and the project file.
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebPage>
#include <QWebFrame>
QWebPage page;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(page.mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded1(bool)));
QUrl router("http://192.168.1.1");
page.mainFrame()->load(router);
}
MainWindow::~MainWindow()
{
delete ui;
}
untitled.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2013-05-01T23:48:00
#
#-------------------------------------------------
QT += core gui webkit webkitwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Error:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!
Program: ...tled-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\debug\untitled.exe
Module: 5.0.2
File: global\qglobal.cpp
Line: 1977
ASSERT: "!"No style available without QApplication!"" in file kernel\qapplication.cpp, line 962
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Extra characters are inserted here to bypass character requirement.
In main.cpp, make sure you create an application object, even if you don't use directly:
QApplication app;
// Below you can then create the window
Edit
The problem is that you are creating a QWebPage as a global object, and before the QApplication has been created. To solve the problem, make the page a member of the MainWindow class. Also make the page a pointer, otherwise you will get other problems.
i.e. in mainwindow.h:
private:
QWebPage* page;
in mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebPage>
#include <QWebFrame>
// Remove this!!
// QWebPage page;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Create the page here:
page = new QWebPage(this);
connect(page.mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded1(bool)));
QUrl router("http://192.168.1.1");
page.mainFrame()->load(router);
}
MainWindow::~MainWindow()
{
delete ui;
}
Related
I am using QStateMachine framework for a device controller class. It works fine in debug mode. But, in the release mode QStateMachine::started() signal is not being emitted. A simple widget project for the problem (form is empty) is below.
Qt Version 5.14.1
Compiler : MSVC 2017, MinGW (both are 64-bit and results are same)
Test.pro
QT += core gui widgets
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStateMachine>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public slots:
void stateMachineStarted();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QStateMachine *stateMachine;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
void MainWindow::stateMachineStarted()
{
qDebug() << "MainWindow::stateMachineStarted()";
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
qDebug() << "MainWindow::MainWindow()";
ui->setupUi(this);
stateMachine = new QStateMachine;
QState *closed = new QState;
QState *setup = new QState;
QState *opened = new QState;
QState *closing = new QState;
stateMachine->addState(closed);
stateMachine->addState(setup);
stateMachine->addState(opened);
stateMachine->addState(closing);
Q_ASSERT(connect(stateMachine, &QStateMachine::started, this, &MainWindow::stateMachineStarted));
stateMachine->setInitialState(closed);
stateMachine->start();
}
MainWindow::~MainWindow()
{
qDebug() << "MainWindow::~MainWindow()";
delete ui;
}
Application output in debug mode (I closed the form after few seconds.)
12:39:09: Starting C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\Test.exe ...
MainWindow::MainWindow()
MainWindow::stateMachineStarted()
MainWindow::~MainWindow()
12:39:11: C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\Test.exe exited with code 0
Application output in release mode (I closed the form after few seconds.)
12:27:51: Starting C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Release\release\Test.exe ...
MainWindow::MainWindow()
MainWindow::~MainWindow()
12:27:53: C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Release\release\Test.exe exited with code 0
In release mode, it is likely that the connection is not made because you wrapped it inside a Q_ASSERT macro.
See Q_ASSERT release build semantics for more informations.
I am connecting the projector to Quectel QCOM application. Now I want to skip that application and create my customized application to control the projector.
I wrote a program based on the guidelines of Quectel to switch off the projector. Following is the code. Even I used the QSerialPort code. But the projector couldn't switch off. However using the command prompt of the Quectel QCOM application, I could control the projector.
Please help me in this guys! I am stuck in this since a month.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec(); //
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include<QSerialPort>
#include<string>
#include<QtGui>
#include <QMessageBox>
using namespace std;
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow() //
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QMessageBox::information(this,"Title here","Hello World");
serial = new QSerialPort(this);
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::NoParity);
if(!serial->open(QIODevice::ReadWrite))
{
qDebug()<<"error: can't open com port";
}
QString command = "WT+LEDE=0\n";
QByteArray x = command.toLocal8Bit();
serial->write(x);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
QMake project file:
#-------------------------------------------------
Project created by QtCreator 2019-11-02T10:55:21
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GUISerialPort
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I'm trying to ask for a password the user in order to access a particular section, my application is fullscreen. The problem is that when the qInputDialog appears also the unity taskbar and the application titlebar appears. I want to avoid this and keep my application fullscreen.
I'm using Qt 5.12.3 on Ubuntu 16.04
Take a look at this simple example:
Main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showFullScreen();
return a.exec();
}
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QInputDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
bool ok;
QString text = QInputDialog::getText(this, tr("Restriscted"),
tr("Password:"), QLineEdit::Password,"",&ok, Qt::FramelessWindowHint);
if (ok && text=="pass")
{
ui->label->setText("ok");
}
}
Seems there is no solution for this at the moment and the issue depends on Unity. At the end I replaced the qInputDialog with a qStackedView page with a qLabel and a qButton on it, then I hide/show the page accordingly.
I am using Qt5.4.3 and at some point, Qt prints out message like this
LEAK: 145 CachedResource
LEAK: 4432 WebCoreNode
I try to display a webpage on a Qt application, but whatever the webpage is, memory always leaks.
Here is the all code of project named"test"(I add the webview control in mainwindow.ui)
#test.pro
QT += core gui webkit
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets webkitwidgets multimedia multimediawidgets
TARGET = EchartDemo
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
INCLUDEPATH += $$PWD
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
DESTDIR = bin
//main.cpp
#include "widget.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.showMaximized();
return a.exec();
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//`widget.cpp`
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->webView->load(QUrl("file:///"+qApp->applicationDirPath()+"/html/hao.html"));
}
Widget::~Widget()
{
delete ui;
}
//hao.html
<meta http-equiv="Refresh" content="0; url=http://www.hao123.com/?1460255739"/><meta property="shurufa:url-navigate" content="985" />
In order to show the webpage normally, you should put the html file to the folder \build-EchartDemo-Desktop_Qt_5_4_2_MinGW_32bit-Debug\bin\html.
Of course, you can change the content of the hao.html file to whatever you like.
Why do I have these memory leaks?
This is a known bug in Qt and these "memory leaks" are only warnings in some cases. You can read more about it here : Qt Bug 40373, and also see the other bug reports mentioning those leaks.
Also, you should consider to use QWebEngineView which is much better ;) (and update to Qt5.6, but it is another story!).
Question asked twice: see Handling multiple ui files in Qt
I am new to Qt framwork , i have been given this simple task:
In the MainWindow , i have a submit button , once its clicked another total different window should appear
i thought of doing this by doing one extra UI file called From.ui file and to switch from MainWindow to Form once submit is clicked , this is my code:
//main.cpp
#include "mainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.setOrientation(MainWindow::ScreenOrientationAuto);
mainWindow.showExpanded();
return app.exec();
}
//MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QtCore/QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: SubmitClicked()
{
Form* f= new Form(this);
f->show();
f->raise();
f->activateWindow();
}
//Form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
this code compiled perfectly , but its not doing as expected , once submit is clicked , nothing is done...
can you please tell me whats wrong ?
It seems that SubmitClicked slot is not connected to clicked event of your button
Put a cout/printf at the top of your SubmitClicked method to make sure that it is called.