Get the list of all QPushButton in Qt - c++

I would like to get the list of all QPushButton in my MainWindow. Actually, I have a QRadioButton, and when I uncheck it, I would like to disable all the QPushButton of my window.
How can I do that ?

Here is a minimal example:
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QDebug>
int main( int argn, char **argc)
{
QApplication app(argn, argc);
// Creating some content
QWidget window;
QPushButton ba(&window); ba.setObjectName("but1");
QPushButton bb(&window);bb.setObjectName("but2");
QLabel l(&window); l.setObjectName("label");
QPushButton bc(&l);bc.setObjectName("but3");
// Getting all buttons
QList<QPushButton *> butts = window.findChildren<QPushButton *>();
qDebug() << butts.size();
for (const auto *but: butts) qDebug() << " " << but->objectName();
return 0;
}

Related

QTabwidget click tab event C++

I am new in QT 4 C++ .I have QT 4 form with QTabwidget like in th picture below.
enter image description here
I want to disply on console the string "aa" by selecting tab on clicking it.
Here is my newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
#include "ui_newForm.h"
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
virtual ~newForm();
private:
Ui::newForm widget;
};
#endif /* _NEWFORM_H */
_________________________________________________________________________________________
Here is my main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm *a = new newForm();
a->show();
// create and show your widgets here
return app.exec();
}
Here is my newForm.cpp
#include "newForm.h"
#include <iostream>
#include <QDebug>
newForm::newForm() {
connect(widget.tabWidget, SIGNAL(stateChanged()), this, SLOT(onTabChanged(int)));
widget.setupUi(this);
}
newForm::~newForm() {
}
void newForm::onTabChanged(int ){
qDebug()<<"aa"<<"\n";
}
On selecting new tab it is not displaying "aa"?How to qDebug() "aa" on console?
First of all, why are you using Qt 4 in 2022?
Next thing use currentIndexChanged(int) istead of stateChanged().
Did you also noticed that stateChanged() doesnt pass an interger which is needed for onTabChanged(int).
You also connected widget.tabWidget which isn't initilized yet.
This code might work:
newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
namespace Ui { class newForm; };
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
~newForm();
private:
Ui::newForm *widget;
};
#endif /* _NEWFORM_H */
newForm.cpp
#include "newForm.h"
#include "ui_newForm.h"
#include <QDebug>
newForm::newForm()
: widget(new Ui::newForm)
{
widget->setupUi(this);
connect(widget->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
}
void newForm::onTabChanged(int ){
qDebug() << "aa";
}
newForm::~newForm() {
delete widget;
}
main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm a;
a.show();
// create and show your widgets here
return app.exec();
}

qt c++ QChart->setGeometry does not work in MainWindow

I created an object with a QChart inside and a MainWindow with 2 QPushButton, when I try to display my QChart, it takes all window. If I want to resize QChart everything works but if I try to move it nothing work (with setGeometry or with setContentMargin).
main.cpp
#include <QApplication>
#include "Mainwindow.h"
#include "MainChart.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow w(1920, 1080);
MainChart chart;
chart.setGeometry(250,0,1670,1080); // >> KO
w.setCentralWidget(chart.get_view());
// w.centralWidget()->setMaximumSize(1670, 1080); >> OK
// w.centralWidget()->setContentsMargins(250,0,0,0); >> KO
w.show();
return app.exec();
}
Other files, I think that won't be useful but if need:
MainWindow.cpp
#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include <QAction>
#include <Qt>
#include <QKeySequence>
#include <QtCore>
#include <iostream>
#include "Mainwindow.h"
MainWindow::MainWindow(int _width, int _height, QMainWindow *parent)
: width(_width), height(_height), QMainWindow(parent)
{
this->setStyleSheet("MainWindow {background-color: rgb(40,40,40);}");
this->setWindowFlags( Qt::CustomizeWindowHint );
this->showFullScreen();
this->setFixedSize(width, height);
configure_new_button();
configure_exit_button();
configure_escape();
}
MainWindow::~MainWindow()
{
free(exit_btn);
free(new_btn);
free(escape);
}
void MainWindow::configure_exit_button()
{
exit_btn = new QPushButton(this);
exit_btn->connect(exit_btn, SIGNAL(clicked()),this, SLOT(exit()));
exit_btn->setGeometry(0, height - 100, 100, 100);
exit_btn->setStyleSheet("QPushButton {background-color: rgb(150,150,150);}");
QFont font = exit_btn->font();
font.setPointSize(32);
exit_btn->setFont(font);
exit_btn->setIcon(QIcon(":/Icons/close.png"));
exit_btn->setIconSize(QSize(65, 65));
exit_btn->show();
}
void MainWindow::configure_new_button()
{
new_btn = new QPushButton(this);
new_btn->setGeometry(0, 0, 100, 100);
new_btn->connect(new_btn, SIGNAL(clicked()),this, SLOT(new_entry()));
new_btn->setStyleSheet("background-color: rgb(0,0,200);" "color: black");
QFont font = new_btn->font();
font.setPointSize(32);
new_btn->setFont(font);
new_btn->setIcon(QIcon(":/Icons/nouveau.png"));
new_btn->setIconSize(QSize(65, 65));
new_btn->show();
}
void MainWindow::configure_escape()
{
escape = new QAction( "text4ESC", this );
escape->setShortcut( Qt::Key_Escape );
escape->setShortcutContext( Qt::WindowShortcut );
connect(escape, SIGNAL(triggered()), this, SLOT(exit()));
addAction(escape);
}
void MainWindow::exit()
{
close();
qApp->quit();
}
void MainWindow::new_entry()
{
std::cout << "coucou !!!" << std::endl;
}
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QObject>
#include <QPushButton>
#include <QEvent>
#include <QKeyEvent>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(int _width, int _height, QMainWindow *parent = 0);
~MainWindow();
void configure_exit_button();
void configure_new_button();
void configure_escape();
private slots:
void exit();
void new_entry();
private:
QPushButton *exit_btn;
QPushButton *new_btn;
QAction *escape;
int width;
int height;
};
#endif //MAINWINDOW_H
Mainchart.cpp
#include "MainChart.h"
MainChart::MainChart(QChart *Parent)
{
QBarSet *set0 = new QBarSet("Altuve");
QBarSet *set1 = new QBarSet("Martine");
QBarSet *set2 = new QBarSet("Bob");
*set0 << 256 << 954 << 752 << 148 << 596 << 214;
*set1 << 586 << 369 << 485 << 874 << 693 << 587;
*set2 << 785 << 963 << 547 << 745 << 657 << 874;
QFont font;
font.setPixelSize(18);
QBarSeries *series = new QBarSeries();
series->append(set0);
series->append(set1);
series->append(set2);
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Mon Super Graphique");
chart->setTitleFont(font);
chart->setTitleBrush(QBrush(qRgb(255,255,255)));
chart->setAnimationOptions(QChart::AllAnimations);
QStringList categories;
categories << "2013" << "2014" << "2015" << "2016" << "2017";
QBarCategoryAxis *axis = new QBarCategoryAxis();
axis->append(categories);
chart->createDefaultAxes();
chart->setAxisX(axis, series);
font.setPixelSize(12);
chart->axisX()->setLabelsBrush(QBrush(qRgb(255,255,255)));
chart->axisX()->setLabelsFont(font);
chart->axisY()->setLabelsBrush(QBrush(qRgb(255,255,255)));
chart->axisY()->setLabelsFont(font);
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
chart->legend()->setLabelColor(qRgb(255,255,255));
chart->legend()->setFont(font);
chart->setBackgroundBrush(QBrush(QRgb(0x0)));
chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
}
MainChart::~MainChart() {}
QChartView * MainChart::get_view()
{
return chartView;
}
Mainchart.h
#ifndef NEW_MAINCHART_H
#define NEW_MAINCHART_H
#include <QtCharts/QChartView>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QBarCategoryAxis>
QT_CHARTS_USE_NAMESPACE
class MainChart : public QChart
{
public:
MainChart(QChart *Parent = 0);
~MainChart();
QChartView *get_view();
private:
QChartView *chartView;
};
#endif //NEW_MAINCHART_H
I think you want to look at using layouts. I'm not entirely positive, but making the chart the window's central widget means it's going to take up all the space -- just the way it's behaving.
It sounds like what you should do is create a widget and consider it a container. It becomes your central widget, and then you add things to it. But without a layout, you're going to get weird resize behavior.
It's really far better to grow accustomed to handling layouts using a layout rather than hard-coded size & locations. You have to use widgets as containers to make things work, but everything I've tried to do I've been able to do.

Read the input and print factorial in Qt GUI Application using C++

Using QTCreator, I created the design of a GUI Application. I want to read the input entered by the user from lineEdit and when pushButton is clicked, it should print the factorial of that entered number on the same page. I've read some tutorials but don't understand how to code this using qtc++.
A minimal example is like that:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void hClicked();
void hTextEdit(const QString& data);
private:
QString m_linedata;
QPushButton button;
QLineEdit lineEdit;
QHBoxLayout layout;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
layout.addWidget(&lineEdit);
layout.addWidget(&button);
this->setLayout(&layout);
connect(&lineEdit, &QLineEdit::textChanged, this, &MainWindow::hTextEdit);
connect(&button, &QPushButton::clicked, this , &MainWindow::hClicked);
}
MainWindow::~MainWindow()
{
}
static unsigned factorial(unsigned n)
{
unsigned result = 1;
for (unsigned i=1; i <= n; i++) {
result *= i;
}
return result;
}
void MainWindow::hClicked()
{
if (m_linedata.size() > 0) {
bool res ;
int toint = m_linedata.toInt(&res);
if (res) {
unsigned fact_result = factorial(toint);
lineEdit.clear();
lineEdit.setText(QString::number(fact_result)); }
}
}
void MainWindow::hTextEdit(const QString &data)
{
m_linedata = data;
}
and main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Just do anything you like with the data passed to the auxillary buffer.

QWebEnginePage does not know its contents size

I have a very simple application using WebEngineView and I just wanted to resize the widget displaying to the contents of the html file. I'm expecting it to be 30 pixels wide. Instead my program prints QSize(0,0) and even worser the widget is not displayed at all.
What I'm doing wrong here?
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto view = new QWebEngineView;
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
auto contentsSize=view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
view->show();
return app.exec();
}
Putting my QWebEngineView into a dialog still doesn't work:
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto dialog = new QDialog;
dialog->setLayout(new QHBoxLayout);
auto view = new QWebEngineView;
dialog->layout()->addWidget(view);
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
auto contentsSize=view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
dialog->show();
return app.exec();
}
I also tried to connect to the signal loadFinished, but there is no effect.
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto dialog = new QDialog;
dialog->setLayout(new QHBoxLayout);
auto view = new QWebEngineView;
dialog->layout()->addWidget(view);
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
QObject::connect(view->page(), &QWebEnginePage::loadFinished, [&view](bool b) {
qDebug() << b;
auto contentsSize = view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
});
dialog->show();
return app.exec();
}

Qt (C++) Pass Variable from Dialog to main.cpp

When i start my Program a Dialog-Window pops up and asks me to enter a name. Once i've entered my Name and press the Button it closes the Dialog and opens the main window.
My question is how i get the variable/ Name i just set in the Dialog into another class / my main.cpp
Main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtDebug>
#include <QtNetwork>
#include <sstream>
#include "mydialog.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Open Dialog
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
//Open Main Window
GW2::MainWindow w;
w.show();
return a.exec();
}
mydialog.cpp
#include "mydialog.h"
#include "ui_mydialog.h"
#include <QDebug>
using namespace std;
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyDialog)
{
ui->setupUi(this);
}
MyDialog::~MyDialog()
{
delete ui;
}
void MyDialog::on_pushButton_clicked()
{
QString MYNAME = ui->lineEdit->text();
close();
}
I can get MYNAME here that works after i press the Button but i need to pass the Variable...
mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
#include <QString>
namespace Ui {
class MyDialog;
}
class MyDialog : public QDialog
{
Q_OBJECT
public:
explicit MyDialog(QWidget *parent = 0);
~MyDialog();
private slots:
void on_pushButton_clicked();
private:
Ui::MyDialog *ui;
};
#endif // MYDIALOG_Hs
I tried using google and search function but didn'T find anything that worked on my project. Hope you can help me. Cheers
Add this in MyDialog:
QString MyDialog::getName()
{
return ui->lineEdit->text();
}
Then do:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Open Dialog
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
// retrieve the name
QString name = mDialog.getName();
//Open Main Window
GW2::MainWindow w;
w.show();
return a.exec();
}
Note that the dialog could be canceled. You should call accept() rather than close() from on_pushButton_clicked() and later test if the dialog was accepted or not:
if ( mDialog.exec() == QDialog::Accepted )
{
QString name = mDialog.getName();
...