Basically I created QListWidget and QPushButton and I want to list current item in the widget when I clicked the button.Until here it is fine. What I want is after clicking that button again, I want to see previous situation(old item!) of that item. How can I do that?
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
for(int i = 0; i < 9; i++ ){
ui->listWidget->addItem(QString::number(i) + " old item!");
}
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
QListWidgetItem *itm = ui->listWidget->currentItem();
itm->setText("new item!");
}
Related
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);
}
I am trying to create a menu app (like windows search) with QCompleter.
I would like to show all items from completer when the QLineEdit is empty.
And it works first time, but when I start typing something into the lineEdit and I delete all characters from lineEdit, and then press Enter I see nothing. Where is my mistake?
My code is below.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
this->wordList << "alpha" << "omega" << "omicron" << "zeta" << "icon";
this->lineEdit = new QLineEdit(this);
completer = new QCompleter(wordList, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
lineEdit->setCompleter(completer);
completer->QCompleter::complete();
ui->setupUi(this);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter))
{
if(lineEdit->text()=="")
{
completer->complete();
}
if(wordList.contains(lineEdit->text(),Qt::CaseInsensitive))
qDebug() <<"CATCH IT";
}
}
Could you please advise me?
You need to reset the completion prefix on the completer.
if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
if(lineEdit->text().isEmpty())
{
lineEdit->completer()->setCompletionPrefix("");
lineEdit->completer()->complete();
}
}
Also if the intent is to only have it populate when return is pressed in the line edit, you will want to create your own line edit to handle that versus using the main window.
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;
}
I am trying to be able to click on a button and have a Pixmap image that I'm using pop up, and when I click the button again, it disappears. Right now when I run my app, the image is already there and when I click the button it disappears. How to fix??
#include "yes.h"
#include "ui_yes.h"
yes::yes(QWidget *parent) :
QDialog(parent),
ui(new Ui::yes)
{
ui->setupUi(this);
}
yes::~yes()
{
delete ui;
}
void yes::on_pushButton_clicked()
{
hide();
}
void yes::on_pushButton_2_clicked()
{
QPixmap popup("qrc:/new/prefix1/Popover for seat help");
ui->label->setPixmap(popup);
}
Looking at the on_pushButton_clicked()
I figured it out. Just make the popup invisible and then set the visibility to the opposite of whatever it already is with the button click.
#include "yes.h"
#include "ui_yes.h"
yes::yes(QWidget *parent) :
QDialog(parent),
ui(new Ui::yes)
{
ui->setupUi(this);
ui->label->setVisible(false);
}
yes::~yes()
{
delete ui;
}
void yes::on_pushButton_clicked()
{
hide();
}
void yes::on_pushButton_2_clicked()
{
QPixmap popup("qrc:/new/prefix1/Popover for seat help");
// ui->label->setPixmap(popup);
ui->label->setVisible(!ui->label->isVisible());
}
I just started using Qt Designer and I can not figure out how to assign the same action to more pushbuttons. I created a gridLayout 17x17, made up of 289 pushbuttons. I would like each pushbutton, if clicked, to change its text. Is it possible?
You want to copy the text of a previously selected button to another button, to do you have to declare an attribute of the class I will call text, the selected text will be saved, I will call the previously selected button previousBtn, then every time you click, you must Write about that variable.
text = previousBtn->text();
We can get all the QLayoutItem of the QGridLayout through the itemAtPosition function, then we get the widget through the widget () function and convert it to qobject_cast, connect it to the onClicked slot, get the button that generates it with sender() and convert it To QPushButton, and place the new text.
*.h
private:
QString text;
private slots:
void onClicked();
*.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int i=0; i < ui->gridLayout->rowCount(); i++)
for(int j=0; j < ui->gridLayout->columnCount(); j++){
QLayoutItem* item = ui->gridLayout->itemAtPosition(i, j);
if(item->widget()){
QPushButton* btn = qobject_cast<QPushButton *>(item->widget());
connect(btn, &QPushButton::clicked, this, &MainWindow::onClicked);
}
}
}
void MainWindow::onClicked()
{
QPushButton *btn = qobject_cast<QPushButton *>(sender());
btn->setText(text);
}