How to run thread from push button? - qt

main.cpp:
#include "mainwindow.h"
#include "myobject.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QThread cThread;
MyObject cObject;
cObject.doSetup(cThread);
cObject.moveToThread(&cThread);
// cThread.start();
return a.exec();
}
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_clicked()
{
// cThread.start();
}
I'm trying to start the thread on on_pushButton_clicked(). In order to do this, I would have to pass cThread to mainwindow through an argument, right? I just want someone to confirm to make sure I'm doing it right.

This seems like one way to do that. The other one would be to connect signal clicked() of button to thread's slot start(Priority) in your application which will be nicer and shorter.

Related

Disable scrolling to left/right/up/down in Qwebengineview

I want my qwebengineview can't scroll to the left/right/up/down.
My situation is like this:
It was asked in Qt Fourm first, however I can't find a solution for this.
QT Fourm link: link
Please see the minimum code example code below. It's not the same website but can also show this problem.
I'm using Mac Os 10.15 and Qt 5.13.1.
Update
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWebEngineView>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QWebEngineView* m_pWebView;
};
#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);
this->setFixedSize(920,580);
m_pWebView = new QWebEngineView(this);
// Set WebView Size
m_pWebView->setGeometry(0,0,920,580);
// Disable Right Click
m_pWebView->setContextMenuPolicy(Qt::NoContextMenu);
m_pWebView->load(QUrl("https://clash.razord.top/#/proxies"));
}
MainWindow::~MainWindow()
{
delete 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();
}

setQuitOnLastWindowClosed(false) has disabled 'x' button on window / dialog QT

In order to get my MessageBox working correctly (and not shutting the whole application) - I have had to setQuitOnLastWindowClosed to False.
This means however that pushing the 'x' button on my QDialog does not exit the application properly. Any help?
Here is the code:
Main.cpp
#include "openingdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
Dialog w;
w.show();
return a.exec();
}
Dialog code:
#include "openingdialog.h"
#include "ui_openingdialog.h"
#include "patientsetup.h"
#include <QDesktopServices>
#include <QUrl>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
connect(ui->guideButton, SIGNAL(clicked(bool)), this, SLOT(guideButtonClicked()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::okButtonClicked()
{
psetup = new pmps_f(this);
psetup->show();
}
void Dialog::guideButtonClicked()
{
QDesktopServices::openUrl(QUrl::fromLocalFile("C:/PMPS/PMPSv1/Instructionsforuse.pdf"));
}
MessageBox code:
void pmps_f::start()
{
QMessageBox msgBox;
msgBox.setText("Pressing OK will start the sedation process with the baseline target effect-site Ce of 0.5mcg/ml");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
int ret = msgBox.exec();
switch(ret) {
case QMessageBox::Ok:
break;
case QMessageBox::Cancel:
break;
}
}
Construction of MainWindow:
pmps_f::pmps_f(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::pmps_f)
{
ui->setupUi(this);
connect(ui->startbutton,SIGNAL(clicked(bool)), this, SLOT(start()));
}

accessing a ui.label from main.cpp in QT

i am trying to take a feedback data which is published from arduino and subscribed to my GUI created in QT.
Now i have main.cpp and mainwindow.cpp.
main.cpp
#include <QtGui>
#include <ros/ros.h>
#include <QApplication>
#include "../include/abc/main_window.hpp"
#include "std_msgs/String.h"
#include <std_msgs/UInt16.h>
#include <QMainWindow>
#include <std_msgs/Float32.h>
void chatterCallback(const std_msgs::UInt16 &fb_msg){
ROS_INFO("Feedback: [%f]", fb_msg.data);
ui.label_6->setText(QString("%1").arg(fb_msg.data));
}
int main(int argc, char **argv) {
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("feedback",1000, chatterCallback);
ros::spinOnce();
QApplication app(argc, argv);
abc::MainWindow w(argc,argv);
w.show();
w.setWindowTitle("GUI for Controlling Servo Motor");
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
int result = app.exec();
return result;}
mainwindow.cpp
namespace abc {
using namespace Qt;
QSerialPort *serial;
MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
: QMainWindow(parent)
, qnode(argc,argv)
{
ui.setupUi(this);
}
MainWindow::~MainWindow() {}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
ui.label_5->setText(QString("%1").arg(value));
msg.data = ui.label_5->text().toUInt();
ROS_INFO("%d", msg.data);
chatter_pub.publish(msg);
ros::spinOnce();
}
main_window.hpp
#ifndef abc_MAIN_WINDOW_H
#define abc_MAIN_WINDOW_H
#include <QtGui/QMainWindow>
#include "ui_main_window.h"
#include "qnode.hpp"
#include <QtSerialPort/QSerialPort>
#include <ros/ros.h>
#include "std_msgs/UInt16.h"
namespace abc {
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(int argc, char** argv, QWidget *parent = 0);
~MainWindow();
public Q_SLOTS:
private:
Ui::MainWindowDesign ui;
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise <std_msgs::UInt16> ("chatter", 1000);
QSerialPort *arduino;
QNode qnode;
};
} // namespace abc
#endif // abc_MAIN_WINDOW_H
Now when i run this code, it shows me that UI is not declared in main.cpp.
I wanted to display the data from feedback to label_6 (TextBox). The data is only available in main.cpp, any suggestions are highly appreciable.
Thanks in advance.
You will have to pass the fb_msg.data as an argument to the MainWindow constructor. Then you can set the UI element in the MainWindow thread.
Alternatively once you create the MainWindow instance in your main, you can emit a signal which you catch and process in the MainWindow thread.
You cannot modify the UI elements from a different thread.

How to use qAction submenu in Qt

I want to implement simple commands like a qDebug() when I click on a sub menu in the mainwindow. I was referring to sample program given along with the Qt 5 IDE (...\Qt\Qt5.2.0\5.2.0\msvc2010\examples\widgets\mainwindows\menus), and using it, I managed to construct the code. I do not receive any compile time or run time errors.
I created the mainwindow.ui using the design mode. It has an object of the QAction class called actionInterval.
But when I click on it, nothing happens, I am not able to implement the command in void interval(). I guess I am not connecting properly. What am I missing here? Please advise.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
void createActions();
private slots:
void interval();
};
#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);
createActions();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions()
{
ui->actionInterval = new QAction(tr("&Interval"), this);
ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
}
void MainWindow::interval()
{
qDebug()<<"inside interval qdialog";
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
void MainWindow::createActions()
{
ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
}
You shouldn't need that ui->actionInterval = new QAction(tr("&Interval"), this); line, the ui->setupUi() handles that for you, so it's potentially causing an incorrect reference so when you do click on it it's not firing correctly.

PopUP Window in Qt

I am creating an application in Qt and I have a problem. I have a main window and I want that when I push a button, a popup window appears. I dont know how can I do it. I tried to call the show() method when I push the button but dont work. I think that I must use the exec() method from QApplication but I dont know how can call it if I created it in the main class.
My classes:
#include "mainwindow.h"
#include "dialog.h"
#include <QApplication>
#include "popup1.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QApplication>
int posiciones[10];
std::string port="";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
for (int i=1; i<10; i++){
if(i==7){
posiciones[i]=90;
}
posiciones[i]=0;
}
//Mandar el vector para mover
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
PopUp:
#include "popup1.h"
#include "ui_popup1.h"
Popup1::Popup1(QWidget *parent) :
QDialog(parent),
ui(new Ui::Popup1)
{
ui->setupUi(this);
}
Popup1::~Popup1()
{
delete ui;
}
Anyone knows how can I show the popup window? Thanks for your time.
Connect your button signal clicked() with exec() slot of your popup window:
connect(pushButton, SIGNAL(clicked()), popupWindow, SLOT(exec()));
Where pushButton - pointer to your button, and popupWindow - pointer to your popup window. You can write this code in QMainWindow constructor.
Include your pop-up class in your MainWindow.h:
include "popup.h"
Define a pointer to your pop-up class in the MainWindow.h:
popup1 *mpPopUp1;
Create an object in the MainWindow.cpp file:
mpPopUp1 = new popup1;
Define a slot in MainWindow.h and call it, for example, showPopUp():
void showPopUp();
Create the slot showPopUp() in your MainWindow.cpp and write the following statement inside it:
mpPopUp1 ->show();
Connect your pushButton to the slot showPopUp():
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(showPopUp()));
Run you app, click on pushButton and voila

Resources