I want to try QWinThumbnailToolBar in Qt 5.2 but it doesn't work !(Program runs but there is no thumbnail !!!!)
//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 <QWinThumbnailToolButton>
#include <QWinThumbnailToolBar>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QWinThumbnailToolBar* thumbnailToolBar;
QWinThumbnailToolButton *playToolButton;
QWinThumbnailToolButton *forwardToolButton;
QWinThumbnailToolButton *backwardToolButton;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
playToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
playToolButton->setEnabled(false);
playToolButton->setToolTip(tr("true"));
playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
forwardToolButton->setEnabled(true);
forwardToolButton->setToolTip(tr("Fast forward"));
forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon));
backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
backwardToolButton->setEnabled(true);
backwardToolButton->setToolTip(tr("Rewind"));
backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward));
thumbnailToolBar->addButton(backwardToolButton);
thumbnailToolBar->addButton(playToolButton);
thumbnailToolBar->addButton(forwardToolButton);
}
MainWindow::~MainWindow()
{
delete ui;
}
//pro file :
QT += core gui winextras multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I think the problem is with these two line :
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
I also tried to use QWidget instead of QMainWindow...
How can I fix it ??
Your code to create QWinThumbnailToolBar is correct, the problem is where you create it. I think creating it in the window constructor is the problem (Maybe because the window handle is not ready yet). You can make something like this:
// main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FrmMain w;
w.show();
w.createThmbBar();
return a.exec();
}
Where createThumbBar() is a public function where you create the QWinThumbnailToolBar as in:
// MainWindow.cpp
void MainWindow::createThmbBar()
{
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
playToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
playToolButton->setEnabled(false);
playToolButton->setToolTip(tr("true"));
playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
forwardToolButton->setEnabled(true);
forwardToolButton->setToolTip(tr("Fast forward"));
forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon));
backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
backwardToolButton->setEnabled(true);
backwardToolButton->setToolTip(tr("Rewind"));
backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward));
thumbnailToolBar->addButton(backwardToolButton);
thumbnailToolBar->addButton(playToolButton);
thumbnailToolBar->addButton(forwardToolButton);
}
Late answer but hopefully it could help anyone faces the same issue later.
The fix that #Ramez proposed works however I am getting a crash on application shutdown due to the windows extra. Is there something special I need to do myself in the destructor? The QWindow has already been deleted when the QWinThumbnailToolBarPrivate::hasHandle() checks for the handle.
Exception thrown: read access violation.
d was 0xFFFFFFFFFFFFFF7F.
QPlatformWindow *QWindow::handle() const
{
Q_D(const QWindow);
return d->platformWindow;
}
Stack Trace Below:
Qt5Guid.dll!QWindow::handle() Line 1929 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::hasHandle() Line 460 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::handle() Line 465 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::nativeEventFilter(const QByteArray & __formal, void * message, long * result) Line 549 C++
Qt5Cored.dll!QAbstractEventDispatcher::filterNativeEvent(const QByteArray & eventType, void * message, long * result) Line 484 C++
[External Code]
Qt5Guid.dll!QWindowPrivate::destroy() Line 1914 C++
Qt5Guid.dll!QWindow::destroy() Line 1864 C++
Qt5Widgetsd.dll!QWidgetPrivate::deleteTLSysExtra() Line 1891 C++
Qt5Widgetsd.dll!QWidget::destroy(bool destroyWindow, bool destroySubWindows) Line 12515 C++
Qt5Widgetsd.dll!QApplication::~QApplication() Line 798 C++
Related
I want to change my application Theme with QRadioButton in the QWidget application.
can anyone help me?
I have a Setting class that has some settings and one of them is changing the theme between dark and light.
it is a C++ app with OpenGL.
void Settings::on_DarkTheme_clicked()
{
}
void Settings::on_lightTheme_clicked()
{
}
what should I have to write in these 2 methods?
I used .qss file like this
int main(int argc, char *argv[])
{
QSurfaceFormat fmt;
fmt.setVersion(3,3);
fmt.setProfile(QSurfaceFormat::CoreProfile);
fmt.setSamples(8);
fmt.setOption(QSurfaceFormat::StereoBuffers);
fmt.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
QSurfaceFormat::setDefaultFormat(fmt);
loadSettings();
App app(argc, argv);
QFile stylesheetFile("./Combinear.qss");
stylesheetFile.open(QFile::ReadOnly);
QString stylesheet = QLatin1String(stylesheetFile.readAll());
app.setStyleSheet(stylesheet);
MainWindow w;
w.show();
const auto returnValue = app.exec();
App::setFinished();
return returnValue;
}
but it didn't Work and I'm confused
First of all, I add a radioButton in my mainwindow.ui .
this is my 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();
signals:
void changedDark();
void changedLight();
private slots:
void on_radioButton_clicked(bool checked);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
and my mainwindow.cpp :
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_radioButton_clicked(bool checked)
{
if (checked)
{
ui->radioButton->setText("Dark");
emit changedDark();
}
else
{
ui->radioButton->setText("Light");
emit changedLight();
}
}
and my main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QObject::connect(&w, &MainWindow::changedDark, &a, [&]
{
/**
* Load the application style
*/
QFile dark_styleFile(":/theme/themes/MaterialDark.qss");
dark_styleFile.open(QFile::ReadOnly);
/**
* Apply the loaded stylesheet
*/
QString style(dark_styleFile.readAll());
a.setStyleSheet(style);
});
QObject::connect(&w, &MainWindow::changedLight, &a, [&]
{
/**
* Load the application style
*/
QFile light_styleFile(":/theme/themes/Ubuntu.qss");
light_styleFile.open(QFile::ReadOnly);
/**
* Apply the loaded stylesheet
*/
QString style(light_styleFile.readAll());
a.setStyleSheet(style);
});
return a.exec();
}
This is my result:
you can clone it from this repo:
https://github.com/parisa-hr/qt-theme-example
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 have the MainWindow class. In the constructor of this class I want to start a new thread that will do some work. But I get this error:
Assert failure in QWidget: "Widgets must be created in the GUI thread."
In this new thread I am not creating any widgets. This is what I have tried so far. Could someone help me on solving this problem? In don't have experience with signals and slots and I will really appreciate some advises.
newThread.h
#ifndef NEWTHREAD_H
#define NEWTHREAD_H
#include <QThread>
#include "mainwindow.h"
class NewThread : public QThread
{
Q_OBJECT
public:
explicit NewThread(QObject *parent = 0);
signals:
public slots:
protected:
void run();
};
#endif // NEWTHREAD_H
newThread.cpp
#include "newthread.h"
NewThread::NewThread(QObject *parent) :
QThread(parent) { }
void NewThread::run(){
MainWindow m;
m.updateInBackground();
}
MainWindow.cpp
MainWindow::MainWindow(QStringList applications, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ReadFromRegistry read;
this->setFixedSize(435,280);
ui->setupUi(this);
appsNames = applications;
this->apps = read.getApplicationsFromRegistry(appsNames);
ui->updateInBackgroundCkb->setChecked(false);
//read from settings.xml the time interval
QString time = RWXml::readSettingsFile();
if(time.compare("-1") != 0){
NewThread th;
while(true){
th.start();
th.sleep(time.toLong(0,10));
}
}
}
EDIT:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringList apps;
QString app = "AppTest1";
apps.append(app);
app = "AppTest2";
apps.append(app);
app = "AppTest3";
apps.append(app);
MainWindow w(apps);
w.create();
w.show();
return a.exec();
}
I instantiate the MainWindow in main. But i need to access the method from MainWindow in the run method of the NewThread. That's why it is instantiated in the NewThread.
EDIT:
void MainWindow::updateInBackground(){
ClientSocket client;
for(Application ap : getApps()){
QString currentVersion = ap.getAppVersion();
QString appCode = ap.getAppCode();
QString appSerial = ap.getAppSerialNo();
client.connect();
QString message = "2//" + currentVersion + "//" + appCode + "//"+ appSerial;
//send message to the server
client.sendMessage(message);
//receiver message from the server
QString received = client.receiveMessage();
//check if the current version is the last one
if(received.compare("0") != 0){
//if is not the last one, set the new version
ap.setAppVersion(received);
//set the update date
ap.setCurrentDate();
//write in windows registry
WriteInRegistry::writeRegistry(ap);
//update the xml file containg the updates of this application
updateXMLFile(ap);
}
}
//read from registry
ReadFromRegistry read;
//populate the grid from the MainWindow with the new data
populateTable(read.getApplicationsFromRegistry(getAppsNames()));
client.closeConnection();
}
Your issue with your code is that you create the mainwindow and the qt application in different threads. The main window seems to be created in your "new thread", whereas the qt application is not.
You also seem to have a circular dependency between the mainwindow constructor and the run method of the thread.
You would need to move the mainwindow creation into your main.cpp which is also a logical place for it.
That being said, please do take a look at the url below and all the references in the post for getting some further thoughts.
How to Use QThread in the Right Way (Part 1)
How to Use QThread in the Right Way (Part 2)
I was trying to add a QWidget while runtime in Qt but It is showing SIGSEV signal received from OS because of segmentation fault.
Here is my code:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_submit_clicked();
private:
Ui::MainWindow *ui;
QLabel *label;
QLineEdit *line_edit;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
if(str=="rana"&&str1=="vivek")
{
label = new QLabel();
label->setText("Success");
MainWindow.layout->addWidget(label);
label->show();
}
else
{
line_edit = new QLineEdit();
line_edit->setText("Sorry");
MainWindow.layout->addWidget(line_edit);
line_edit->show();
}
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I know that segmentation fault occurs due to dereferencing of a null pointer but i couldn't find where I have done that mistake.Any Suggestions?
MainWindow.layout->addWidget(label);
doesn't make a lot of sense - this should not even compile, as Sebastian noted.
First, make sure you have layout in the Ui file (I added one vertical layout named verticalLayout), so you have a layout where you will add widgets. You will have a pointer to it inside your ui object.
Now, just use addWidget on that layout and everything should work:
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
if(str=="rana"&&str1=="vivek")
{
QLabel *label = new QLabel();
label->setText("Success");
ui->verticalLayout->addWidget(label);
// label->show(); widgets will became the part of the MainWindow, as the addWidget
// will add them into the hierarchy.
}
else
{
QLineEdit *line_edit = new QLineEdit();
line_edit->setText("Sorry");
ui->verticalLayout->addWidget(line_edit);
// line_edit->show()
}
}
Note - addWidget will set the owner of the widget to be the layout, so the widget will be deleted on the destruction of the layout.
Maybe implementing in this way will make sense?
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
QWidget *w = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout; // creates a vertical layout
if(str=="rana"&&str1=="vivek")
{
label = new QLabel(w);
label->setText("Success");
layout->addWidget(label);
}
else
{
line_edit = new QLineEdit(w);
line_edit->setText("Sorry");
layout->addWidget(line_edit);
}
w->setLayout(layout);
setCentralWidget(w);
}
UPDATE:
QMainWindow already has a predefined layout, so it was needless to introduce a new one. The code above creates an intermediate widget and construct it using its own layout. Than the widget set as a central widget in the MainWindow.