I've set up a basic Qt-Widgets-Application (Qt 5.5 community) with a simple QWidget "MainWindow" and an additinal QWidget "SettingsScreen".
Within the "MainWindow", touchscreen-events (handled by OS) are working as expected, but after opening the "SettingsScreen" all touch-events are executed on the desktop until I close the "SettingsScreen" using mouse or keyboard.
Environment:
Ubuntu Studio 14.04.03
Qt 5.5 Open Source Edition
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <settingsscreen.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btnExit_clicked();
void on_btnSettings_clicked();
private:
Ui::MainWindow *ui;
SettingsScreen *wSettingsScreen;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settingsscreen.h"
#include "ui_settingsscreen.h"
MainWindow::MainWindow(QWidget *parent) : QWidget(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnExit_clicked()
{
this->close();
}
void MainWindow::on_btnSettings_clicked()
{
wSettingsScreen = new SettingsScreen(parentWidget());
wSettingsScreen->show();
}
settingsscreen.h
#ifndef SETTINGSSCREEN_H
#define SETTINGSSCREEN_H
#include <QWidget>
namespace Ui {
class SettingsScreen;
}
class SettingsScreen : public QWidget
{
Q_OBJECT
public:
explicit SettingsScreen(QWidget *parent = 0);
~SettingsScreen();
private slots:
void on_pushButton_clicked();
void on_btnBack_clicked();
private:
Ui::SettingsScreen *ui;
};
#endif // SETTINGSSCREEN_H
settingsscreen.cpp
#include "settingsscreen.h"
#include "ui_settingsscreen.h"
SettingsScreen::SettingsScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::SettingsScreen)
{
ui->setupUi(this);
}
SettingsScreen::~SettingsScreen()
{
delete ui;
}
void SettingsScreen::on_btnBack_clicked()
{
this->close();
}
I've just started developing with Qt, so please forgive me if I'm missing something essential :)
Any help would by highly appreciated!!
Thank you in advance!
Actually the thing is QMainwindow or any base Widget of your application is able to properly synthesize the Unhandled touch screen events to Mouse events. So whenever you are creating a dialog/widget, make sure to set Mainwindow as there parent and in the constructor of the child widget use setParent(parent). Even I was facing this kind of Issue and this worked for me.
Related
I try to switch between two different Widgets in QT5.
The first problem was that my first window was visible in the background of my second window. I solve this with a check in "autoFillBackground". Now i can switch between them, but if i resize them it only resize the Main content.
Both Widgets have a grid layout.
Resize Problem
Im new at QT5, so is there a better way to make a switching between 2 widgets without this problem?
I try it with wMessages = new Messages(); and hide() but then my program crash after going back.
Code:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "messages.h"
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_btnMessages_clicked();
private:
Ui::MainWindow *ui;
Messages *wMessages;
};
#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_btnMessages_clicked()
{
wMessages = new Messages(this);
wMessages->show();
}
messages.h:
#ifndef MESSAGES_H
#define MESSAGES_H
#include <QWidget>
namespace Ui {
class Messages;
}
class Messages : public QWidget
{
Q_OBJECT
public:
explicit Messages(QWidget *parent = nullptr);
~Messages();
QString NewsGenerator();
private slots:
void on_bBack_clicked();
void on_pushButton_clicked();
private:
Ui::Messages *ui;
};
#endif // MESSAGES_H
messages.cpp:
#include "messages.h"
#include "ui_messages.h"
Messages::Messages(QWidget *parent) :
QWidget(parent),
ui(new Ui::Messages)
{
ui->setupUi(this);
}
Messages::~Messages()
{
delete ui;
}
void Messages::on_bBack_clicked()
{
this->close();
QWidget *parent = this->parentWidget();
parent->show();
}
Edit 1 - working Main Window Button (from G.M.´s answer):
void MainWindow::on_btnMessages_clicked()
{
wPlaner = new Planer();
QMainWindow::setCentralWidget(wPlaner);
}
Update (based on the comments):
The best solution was to use the QStackedWidget to navigate between the views.
Docu: https://doc.qt.io/qt-5/qstackedwidget.html#details
Example-Code for Button:
// return back to first view
void MainWindow::on_btnret_clicked()
{
ui->stackedWidget->setCurrentIndex(0);
}
Welcome, I have a question about Qt's windows operations. I have a simple app which contains 2 windows:
MainWindow - includes push button, can't be resizeable,
AdminWindow - includes label, can be resizeable.
When I click push button, it should open AdminWindow and hide MainWindow. I made the app and it seems to work but when I open the AdminWindow, the application icon which is located in windows's bottom bar is missing. How can I fix it?
Icon is showed when MainWindow is opened:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "adminwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButtonOpenAdmin_clicked();
private:
Ui::MainWindow *ui;
AdminWindow *adminWindow;
};
#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;
}
// hides MainWindow and show AdminWindow
void MainWindow::on_pushButtonOpenAdmin_clicked()
{
hide();
adminWindow = new AdminWindow(this);
adminWindow->show();
}
adminwindow.h
#ifndef ADMINWINDOW_H
#define ADMINWINDOW_H
#include <QMainWindow>
namespace Ui {
class AdminWindow;
}
class AdminWindow : public QMainWindow
{
Q_OBJECT
public:
explicit AdminWindow(QWidget *parent = 0);
~AdminWindow();
private:
Ui::AdminWindow *ui;
};
#endif // ADMINWINDOW_H
adminwindow.cpp
#include "adminwindow.h"
#include "ui_adminwindow.h"
AdminWindow::AdminWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AdminWindow)
{
ui->setupUi(this);
}
AdminWindow::~AdminWindow()
{
delete ui;
}
I am trying to connect a signal from a second QMainWindow to the mainwindow. It doesn't say anything about a problem connection when the program is launched, but It doesn't work. I am not very familiar with C++ and Qt so maybe is something simple.
My code consists on a Mainwindow used as a SCADA with Start, stop, On, off buttons. In the second qmainwindow I created a terminal where you can type, start,stop... There, I would like to emit a signal to my MainWindow which is in charge of controlling the multiple threads and windows. The problem is that I cannot connect to my slot. I present here a simple overview of this two pieces of code.
Terminal. h
#ifndef TERMINAL__H
#define TERMINAL__H
#include <QMainWindow>
#include <QTextEdit>
#include <QLineEdit>
#include <QObject>
namespace Ui {
class Terminal_;
}
class Terminal_ : public QMainWindow
{
Q_OBJECT
public:
explicit Terminal_(QWidget *parent = 0);
~Terminal_();
signals:
void turnonPLC_terminal();
public slots:
void newline();
private:
Ui::Terminal_ *ui;
QTextEdit* mTerminal;
QLineEdit* mInput;
};
#endif // TERMINAL__H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "terminal_.h"
#include "terminal_help.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Terminal_ *terminal;
public slots:
void turnon_terminal();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "terminal_.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
terminal = new Terminal_(this);
connect(terminal, SIGNAL(turnonPLC_terminal()), this, SLOT(turnon_terminal()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::turnon_terminal(){
turnonPLC=1;
}
terminal_.cpp
#include "terminal_.h"
#include "ui_terminal_.h"
#include <QDockWidget>
#include <QWidget>
#include <QLineEdit>
QString on=("on");
Terminal_::Terminal_(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Terminal_)
{
ui->setupUi(this);
mTerminal = new QTextEdit();
setCentralWidget(mTerminal);
mInput = new QLineEdit();
QDockWidget* qdw = new QDockWidget;
qdw->setWidget(mInput);
addDockWidget(Qt::BottomDockWidgetArea, qdw);
connect (mInput, SIGNAL(returnPressed()),
this, SLOT(newline()));
}
Terminal_::~Terminal_()
{
delete ui;
}
void Terminal_::newline(){
QString command = mInput->text();
if (command==on){
emit turnonPLC_terminal();
}
}
Thanks
The signal-slots part in the code works perfectly. (compiled and tested with some small modifications)
After entering "on" (not On as written in question)
Terminal_::newline() slot called, turnonPLC_terminal() is fired and finally
void MainWindow::turnon_terminal() is called.
However, there are some small details the header file is called terminal_.h, not Terminal.h turnonPLC is not defined. terminal is created by not displayed (no show-call).
I guess, there are simply some many small logic errors. Try to use debugger or trace the chain of expected calls with qDebug.
I am using Qt in my project and facing some difficulty in accessing ui label from other class,I have mainwindow and Yar class as shown below.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "yar.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Yar b;
private:
Ui::MainWindow *ui;
public slots:
void dispal();
private slots:
void on_pushButton_clicked();
};
#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);
QObject::connect(ui->pushButton,SIGNAL(clicked()),&b,SLOT(lll()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dispal(){
ui->label->setText("hello");
}
Yar.h
#ifndef YAR_H
#define YAR_H
#include <QObject>
class Yar : public QObject
{
Q_OBJECT
public:
explicit Yar(QObject *parent = 0);
public slots:
void lll();
};
#endif // YAR_H
void MainWindow::on_pushButton_clicked()
{//ui->label->setText("hello");
//b.wrrrot();
//dispal();
}
Yar.cpp
#include "yar.h"
#include <iostream>
Yar::Yar(QObject *parent) :
QObject(parent)
{
//setupUi(this);
//QObject::connect(&a, SIGNAL(dis), &a, SLOT(dispal()));
//emit wrrrot();
}
void Yar::lll(){
//emit wrrrot();
std::cout<<"gfggdf"<<std::endl;
}
In my gui i have one push button and one label, I have connected push button with lll() function of class Yar and when I click push button it is displaying gfggdf in console but I want to display this text in ui label, could you please help me how can I display my data in ui label from function lll();
One possible solution is to emit signal from Yar::lll() i.e.
emit updateLabelText("your text");
And connect this signal to the slot that handles GUI (changes label with given text) in the MainWindow.
I'm new to C++ and QT creator. I've been looking through certain tutorials and understand certain concepts, however, I'm having a hard time applying them or even knowing where to begin. Right now, I'm trying to develop a slideshow-like UI in QT, so basically, it looks into a series of images and loops through them like a video. Ideally having a PLAY and a STOP button(I've read about possibly using QTimer) so far I've only been able to implement when I press one button, the image will appear and when I press the second button, the image will clear. Any direction is appreciated.
in mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pixmap = QPixmap("/Users/Maggie/Desktop/test/char.png");
connect(ui->SetImagePushButton1, SIGNAL(clicked()), this,SLOT(on_SetImagePushButton1_clicked()));
connect(ui->SetImagePushButton2, SIGNAL(clicked()), this,SLOT(on_SetImagePushButton2_clicked()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_SetImagePushButton1_clicked()
{
ui->pictureBox->setPixmap(pixmap);
}
void MainWindow::on_SetImagePushButton2_clicked()
{
ui->pictureBox->setPixmap(QPixmap());
}
in mainwindow.h
#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();
private slots:
void on_SetImagePushButton1_clicked();
void on_SetImagePushButton2_clicked();
private:
Ui::MainWindow *ui;
QPixmap pixmap;
};
#endif // MAINWINDOW_H