Skip to content

Instantly share code, notes, and snippets.

@yamachu
Created November 2, 2016 08:19
Show Gist options
  • Select an option

  • Save yamachu/e06cc536ba3b8cc719c008fe42043f4c to your computer and use it in GitHub Desktop.

Select an option

Save yamachu/e06cc536ba3b8cc719c008fe42043f4c to your computer and use it in GitHub Desktop.
Teratail_53681_Qt
#include "anotherthreadworker.h"
#include <QPixmap>
#include <mainwindow.h>
#include <QDebug>
#include <QFile>
AnotherThreadWorker::AnotherThreadWorker(QObject *parent) : QObject(parent)
{
}
void AnotherThreadWorker::doDirectCall()
{
// it will crash
qDebug() << __FUNCTION__ << QThread::currentThreadId();
auto pixmap = mainWindow->getMainWindowWidgetPixmap();
}
void AnotherThreadWorker::doSignalCall()
{
qDebug() << __FUNCTION__ << QThread::currentThreadId();
emit requestGrabWidget();
}
void AnotherThreadWorker::onGrabRecieved(QPixmap pixmap)
{
qDebug() << __FUNCTION__ << QThread::currentThreadId();
QFile file("another_thread.png");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");
}
#ifndef ANOTHERTHREADWORKER_H
#define ANOTHERTHREADWORKER_H
#include <QObject>
#include <QThread>
#include <mainwindow.h>
class AnotherThreadWorker : public QObject
{
Q_OBJECT
public:
explicit AnotherThreadWorker(QObject *parent = 0);
void doDirectCall();
void doSignalCall();
void setMainWindow(MainWindow *window) {
mainWindow = window;
}
signals:
void requestGrabWidget();
public slots:
void onGrabRecieved(QPixmap pixmap);
private:
MainWindow* mainWindow;
};
#endif // ANOTHERTHREADWORKER_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QThread>
#include "anotherthreadworker.h"
#include <QFile>
void MainWindow::grabMainWindowWidget() {
auto pix = this->grab(this->rect());
emit onFinishedGrabWidget(pix);
}
void MainWindow::onRecievedGrabRequest()
{
qDebug() << __FUNCTION__ << QThread::currentThreadId();
grabMainWindowWidget();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto workerObject = new AnotherThreadWorker();
auto thread = new QThread();
workerObject->moveToThread(thread);
thread->start();
qDebug() << "Curernt thread id is " << QThread::currentThreadId();
auto buttonMain = new QPushButton("Call main thread button");
connect(buttonMain, &QPushButton::clicked, [=](bool checked)
{
qDebug() << "Clicked on " << QThread::currentThreadId();
// This call on UI thread, so success
auto pixmap = getMainWindowWidgetPixmap();
QFile file("main_thread.png");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");
});
auto buttonThread = new QPushButton("Call another thread button");
connect(buttonThread, &QPushButton::clicked, [=](bool checked){
workerObject->doDirectCall();
});
auto buttonSignal = new QPushButton("Call another thread with signal button");
connect(buttonSignal, &QPushButton::clicked, [=](bool checked){
workerObject->doSignalCall();
});
connect(workerObject, SIGNAL(requestGrabWidget()) ,this, SLOT(onRecievedGrabRequest()));
connect(this, SIGNAL(onFinishedGrabWidget(QPixmap)), workerObject, SLOT(onGrabRecieved(QPixmap)));
auto layout = new QVBoxLayout(this);
layout->addWidget(buttonMain);
layout->addWidget(buttonThread);
layout->addWidget(buttonSignal);
ui->centralWidget->setLayout(layout);
}
MainWindow::~MainWindow()
{
delete ui;
}
QPixmap MainWindow::getMainWindowWidgetPixmap()
{
return this->grab(this->rect());
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPixmap getMainWindowWidgetPixmap();
private:
void grabMainWindowWidget();
public slots:
void onRecievedGrabRequest();
signals:
void onFinishedGrabWidget(QPixmap pixmap);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-02T15:59:39
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Teratail_53681_Qt
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
anotherthreadworker.cpp
HEADERS += mainwindow.h \
anotherthreadworker.h
FORMS += mainwindow.ui
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment