stack widget pages using pushbutton in Qt - qt

How to navigate QStackWidget pages using QPushButtons?
Which method needs to be called whenever pushbutton is clicked, so that it opens a particluar stackwidget page.
Thanks & Regards,

QStackedWidget has a method .setCurrentIndex(int) which you can use to switch to a different page. You can use that method in combination with clicked() signal of QPushButton to change the current stacked widget page.
Example:
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
//initially set current page to 0
ui->stackedWidget->setCurrentIndex(0);
//suppose we have buttons button1 and button2
connect(button1, &QPushButton::clicked, this, [=]() {
//set page to 1
ui->stackedWidget->setCurrentIndex(1);
});
connect(button2, &QPushButton::clicked, this, [=]() {
//set page to 2
ui->stackedWidget->setCurrentIndex(2);
});
}
You can also use normal function slots instead of lambda functions:
//mainwindow.h file
class MainWindow{
//...
private slots:
void onButton1Clicked();
void onButton2Clicked();
}
//mainwindow.cpp file
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
//...
connect(button1, &QPushButton::clicked, this, &MainWindow::onButton1Clicked);
connect(button2, &QPushButton::clicked, this, &MainWindow::onButton2Clicked);
}
void MainWindow::onButton1Clicked()
{
ui->stackedWidget->setCurrentIndex(1);
}
void MainWindow::onButton2Clicked()
{
ui->stackedWidget->setCurrentIndex(1);
}

Related

Something seems wrong while dragging QSplitter?

A qsplitter has been used in my application, and OpaqueResize is set to 'false',while dragging the splitter handle, the widget becoming smaller is repainted continuously, this is not right. What I thought is that the QWidgets on both sides of the splitter handle should not receive any signals about repaint or resize until mouseRelease.This issue troubles me several days, if anyone knows about how to deal with it, please give me a hand, thank you.
Qt5.11.2 on Windows10 64-bits
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// left
ui->widget_2->bgColor = QColor(255,0,0);
ui->widget_2->tag = 0;
// right
ui->widget_3->bgColor = QColor(0,0,255);
ui->widget_3->tag = 1;
}
MainWindow::~MainWindow()
{
delete ui;
}
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
bgColor = QColor(255,255,255);
tag = 0;
}
void MyWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent( event );
QPainter painter(this);
QBrush brush( bgColor );
painter.fillRect( event->rect(), brush );
qDebug() << "paintEvent:tag="<<tag;
}

Unexpected behavior on QWidget while the event filtering

Just i've tested the following codes on QPushButton and on QWidget and i've encountered with different behaviors. But why?
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->widget->setStyleSheet("background:blue;");
ui->pushButton->setStyleSheet("background:blue;");
ui->widget->installEventFilter(this);
ui->pushButton->installEventFilter(this);
...
}
bool MainWindow::eventFilter(QObject* watched, QEvent* event)
{
if (watched==ui->pushButton && event->type()==QEvent::Paint)
{
// Do nothing
return true;
}
else if (watched==ui->widget && event->type()==QEvent::Paint)
{
// Do nothing
return true;
}
else
return QMainWindow::eventFilter(watched, event);
}
Then, the pushButton has disappeared as normally, because i've masked its paintEvent with eventFilter. But the widget has painted to blue. Why widget hasn't disappeared.
I've solved the problem with changing QEvent::Paint with QEvent::Polish.
To who wanted the detailed description
Thanks everybody.

How to connect LineEdit and PushButton in Qt?

I created a .ui file using Qt Designer and in the file I created a PushButton which is disabled initially, I also have a LineEdit. I want to connect LineEdit and PushBotton so that when text changed in LineEdit , the PushButton will be enabled, But I don't find any such option in Signals and slots. Can anyone help?
You have to write a custom slot (which is pretty easy).
Add this to your MainWindow declaration (.h file):
private slots:
void checkInput(const QString &text);
Your .cpp file:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(checkInput(QString)));
}
void MainWindow::checkInput(const QString &text)
{
ui->pushButton->setEnabled(!text.isEmpty());
}
To add this slot to Qt Designer, do the following:
Right click on your MainWindow, "Change signals/slots";
Add your custom slot ("Plus" button) by entering checkInput();
After this you will be able to connect your custom slot via Qt Designer.
In Qt 5, you generally don't need trivial private slots and should use lambdas instead:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->lineEdit, &QLineEdit::textChanged, [this](const QString & text) {
ui->pushButton->setEnabled(!text.isEmpty());
});
...
}

How to display a widget inside a main window

I have a project marines and i have the following files structure.
marines.pro
FORMS
iran.ui
marines.h
Headers
iran.h
marines.h
Sources
iran.cpp
main.cpp
marines.cpp
I added the widget iran in the project marines.
Here is marines.cpp
#include <QtGui>
#include "marines.h"
#include "iran.h"
marines::marines(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::marines)
{
ui->setupUi(this);
connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->actionIran, SIGNAL(triggered()), this, SLOT(ir()));
}
void marines::ir()
{
//slot to display iran ui inside my main window
}
marines::~marines()
{
delete ui;
}
and here is my iran.cpp
#include "iran.h"
#include <QtGui>
iran::iran(QWidget *parent) :
QWidget(parent),
ui(new Ui::iran)
{
ui->setupUi(this);
}
iran::~iran()
{
delete ui;
}
How can i display the widget iran i made in qt designer?.
It all depends on how you want the widget to be displayed.
you could add a layout to your central widget in your MainWindow and add your custom widget to the layout
if you want your custom widget to be centralWidget of the MainWindow then use setCentralWidget
If you want the custom widget as a subWindow then add MdiArea to your MainWindow. Then add custom widget to you MdiArea.
If you just want the custom widget to be displayed as a window then just "widget.show()"
Its better to look at Qt's examples to understand how a MainWindow is used.
marines::marines(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::marines)
{
ui->setupUi(this); // after this
iran *ir = new iran(); // create variable ir
ir->show(); // show window
...
}

QLineEdit: how to handle up and down arrows?

I have a console input in my Qt based application, it's a QLineEdit, all Ui is designed via QtDesigner. Is it any easy way way to handle up and down arrows in order to implement input history? The 'go to slot' only show returnProcessed signal, no way i can see to handle up and down arrows :(
you can install event filter and watch your line edit event in your window class. Below is an example:
declare event handler method on your window class:
class MainWindow : public QMainWindow {
Q_OBJECT
...
protected:
void changeEvent(QEvent *e);
...
};
window constructor
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
...
ui->lineEdit->installEventFilter(this);
}
event handler implementation:
bool MainWindow::eventFilter(QObject* obj, QEvent *event)
{
if (obj == ui->lineEdit)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Up)
{
qDebug() << "lineEdit -> Qt::Key_Up";
return true;
}
else if(keyEvent->key() == Qt::Key_Down)
{
qDebug() << "lineEdit -> Qt::Key_Down";
return true;
}
}
return false;
}
return QMainWindow::eventFilter(obj, event);
}
hope this helps, regards
You can subclass QLineEdit and re-implement the virtual keyPressEvent method to handle your special keys.
void MyLineEdit::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Up){
// move back in history
}
else if(event->key() == Qt::Key_Down){
// move forward in history
}
else{
// default handler for event
QLineEdit::keyPressEvent(event);
}
}
I had the same problem, but I find out in other forums that you need to setFocus, e.g.:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
...
ui->lineEdit->installEventFilter(this);
this->setFocus();
}
It works for me.
Reference:
http://www.qtforum.org/article/28240/how-to-get-arrow-keys.html
For me in PyQt this was not working,
class MainWidget(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi()
self->installEventFilter(self)
But this worked,
class MainWidget(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi()
QtGui.QApplication.instance().installEventFilter(self)

Resources