calculate BMI program - c++

This is a program that calculates the BMI by inputting weight and height
#ifndef BMIVIEWER_H
#define BMIVIEWER_H
#include <QWidget>
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QLCDNumber>
#include <QErrorMessage>
#include <QString>
#include <QMessageBox>
class BmiViewer : public QWidget {
Q_OBJECT
public:
BmiViewer();
void calculateBmi();
private:
QLineEdit* heightEntry;
QLineEdit* weightEntry;
QLCDNumber* result;
QErrorMessage* error;
};
#endif // BMIVIEWER_H
bmiviewer.cpp
#include "bmiviewer.h"
BmiViewer::BmiViewer(){
setWindowTitle("MMI Calculator");
QGridLayout* layout = new QGridLayout;
QLabel* inputWeightRequest = new QLabel ("Enter weight in Kg:");
weightEntry = new QLineEdit;
QLabel* inputHeightRequest = new QLabel ("Enter height in meters:");
heightEntry = new QLineEdit;
QPushButton* calc = new QPushButton ("Calculate");
QLabel* labelbmi = new QLabel ("BMI");
result = new QLCDNumber;
result->setSegmentStyle(QLCDNumber::Flat);
result->setDigitCount(8);
layout->addWidget (inputWeightRequest, 0,0);
layout->addWidget (weightEntry, 0,1);
layout->addWidget (inputHeightRequest, 1,0);
layout->addWidget (heightEntry, 1,1);
layout->addWidget (calc, 2,1);
layout->addWidget (labelbmi, 3,0);
layout->addWidget (result, 3,1);
setLayout(layout);
//connect signals and slots
connect(calc,SIGNAL(clicked()),this, SLOT(calculateBmi()));
}
void BmiViewer::calculateBmi(){
int wanswer=0;
int hanswer=0;
double bmi;
QString iStr = weightEntry->text();
QString iStrh = heightEntry->text();
bool ok;
wanswer = iStr.toInt(&ok);
hanswer = iStrh.toInt(&ok);
if (!ok) {
error = new QErrorMessage(this);
error->showMessage("Please enter a valid integer");
return;
}
//calculate BMI
bmi=wanswer/(hanswer*hanswer);
result->display(bmi);
}
main.cpp
#include <QApplication>
#include "bmiviewer.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
BmiViewer w;
w.show();
return a.exec();
}
When I compile it it outputs: Object::connect: No such slot BmiViewer::calculateBmi() in bmiviewer.cpp:29
It displays the interface but no calculations are done.

Add the line
public slots:
before
void calculateBmi();
You never declared the bmi function a slot and so it can't be connected to.

Related

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.

user interface inside QSystemTrayIcon context menu

I'm creatig tray icon application and i want create advanced context menu, like on pictures below, but i only know, how to create simple menues with
QMenu* menu = new QMenu()
menu->addAction(QIcon(), "item", item1Click);
trayIcon->setContextMenu(menu);
How can i do this?
Well, is supose, it's better to show you code:
main.h
#ifndef MAIN_H
#define MAIN_H
#include <QtWidgets/QApplication>
#include <QtCore/QDebug>
#include <QtGui/QIcon>
#include <QtWidgets/QSystemTrayIcon>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QWidgetAction>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QLabel>
class SpinBoxAction : public QWidgetAction
{
public:
SpinBoxAction (const QString& title) : QWidgetAction (NULL)
{
QWidget* Widget = new QWidget (NULL);
QHBoxLayout* Layout = new QHBoxLayout();
QLabel* Label = new QLabel (title);
Layout->addWidget (Label);
SpinBox = new QSpinBox(NULL);
Layout->addWidget (SpinBox);
Widget->setLayout (Layout);
setDefaultWidget(Widget);
}
QSpinBox* spinBox()
{
return SpinBox;
}
private:
QSpinBox* SpinBox;
};
class Reciever : public QObject
{
private:
QSystemTrayIcon* trayIcon;
public:
Reciever()
{
}
void setup(QSystemTrayIcon* trayIcon)
{
this->trayIcon = trayIcon;
}
Q_OBJECT
public slots:
void action(int i)
{
trayIcon->showMessage("changed", "spin box value has been changed", QSystemTrayIcon::NoIcon, 1000);
}
void onActivated(QSystemTrayIcon::ActivationReason reason)
{
trayIcon->showMessage("activated", "tray icon has been activated", QSystemTrayIcon::NoIcon, 1000);
}
};
#endif // MAIN_H
main.cpp
#include <main.h>
#include <QtWidgets/QApplication>
#include <QtCore/QDebug>
#include <QtGui/QIcon>
#include <QtWidgets/QSystemTrayIcon>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QWidgetAction>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QHBoxLayout>
int main(int argc, char** argv)
{
Reciever* reciever = new Reciever();
QApplication app(argc, argv);
QSystemTrayIcon* trayIcon = new QSystemTrayIcon(QIcon(":/images/abc.png"));
if (!trayIcon->isSystemTrayAvailable()) exit(1);
QMenu* menu = new QMenu();
SpinBoxAction* spinBoxAction = new SpinBoxAction("Action Title");
menu->addAction(spinBoxAction);
QObject::connect(spinBoxAction->spinBox(), SIGNAL(valueChanged(int)), reciever, SLOT(action(int)));
trayIcon->setContextMenu(menu);
trayIcon->setVisible(true);
QObject::connect(trayIcon, &QSystemTrayIcon::activated, reciever, &Reciever::onActivated);
reciever->setup(trayIcon);
return app.exec();
}
And it leads to simple list menu with one empty element:

QOpenGLWidget problems under Fullscreen

I'm using Qt 5.4.0.
I added two widgets in QMainWindow: QOpenGLWidget and QQuickView. But my problem is when I clicked on fullscreen button, QQuickView would be disappeared and then when I maximized it, QQuickView would be appeared on the screen. what did i do wrong?
my code:
helper.h
#include <QBrush>
#include <QFont>
#include <QPen>
#include <QWidget>
class Helper
{
public:
Helper();
public:
void paint(QPainter *painter, QPaintEvent *event, int elapsed);
private:
QBrush background;
QBrush circleBrush;
QFont textFont;
QPen circlePen;
QPen textPen;
};
helper.cpp
#include <QPainter>
#include <QPaintEvent>
#include <QWidget>
Helper::Helper()
{
QLinearGradient gradient(QPointF(50, -20), QPointF(80, 20));
gradient.setColorAt(0.0, Qt::white);
gradient.setColorAt(1.0, QColor(0xa6, 0xce, 0x39));
background = QBrush(QColor(64, 32, 64));
circleBrush = QBrush(gradient);
circlePen = QPen(Qt::black);
circlePen.setWidth(1);
textPen = QPen(Qt::white);
textFont.setPixelSize(50);
}
void Helper::paint(QPainter *painter, QPaintEvent *event, int elapsed)
{
painter->fillRect(event->rect(), background);
painter->translate(100, 100);
painter->save();
painter->setBrush(circleBrush);
painter->setPen(circlePen);
painter->rotate(elapsed * 0.030);
qreal r = elapsed / 1000.0;
int n = 30;
for (int i = 0; i < n; ++i) {
painter->rotate(30);
qreal factor = (i + r) / n;
qreal radius = 0 + 120.0 * factor;
qreal circleRadius = 1 + factor * 20;
painter->drawEllipse(QRectF(radius, -circleRadius,
circleRadius * 2, circleRadius * 2));
}
painter->restore();
painter->setPen(textPen);
painter->setFont(textFont);
painter->drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, QStringLiteral("Qt"));
}
glwidget.h
#include <QOpenGLWidget>
class Helper;
class GLWidget : public QOpenGLWidget
{
Q_OBJECT
public:
GLWidget(Helper *helper, QWidget *parent);
public slots:
void animate();
protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
Helper *helper;
int elapsed;
};
glwidget.cpp
#include "glwidget.h"
#include "helper.h"
#include <QPainter>
#include <QTimer>
GLWidget::GLWidget(Helper *helper, QWidget *parent)
: QOpenGLWidget(parent), helper(helper)
{
elapsed = 0;
setFixedSize(200, 200);
setAutoFillBackground(false);
}
void GLWidget::animate()
{
elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000;
update();
}
void GLWidget::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
helper->paint(&painter, event, elapsed);
painter.end();
}
mainwindow.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
bool _showFullScreen;
Helper helper;
};
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QQuickView>
#include <QQuickItem>
#include <QTimer>
#include "glwidget.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
_showFullScreen = false;
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(200, 200);
container->setMaximumSize(200, 200);
container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("QquickView.qml"));
ui->verticalLayout->addWidget(container);
GLWidget *openGL = new GLWidget(&helper, this);
ui->verticalLayout_2->addWidget(openGL);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), openGL, SLOT(animate()));
timer->start(50);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
if (_showFullScreen == false) {
showFullScreen();
_showFullScreen = true;
} else {
showMaximized();
_showFullScreen = false;
}
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Emit Signal Only When QCheckBox is Checked

I am creating a set of QCheckBox dynamically based on some user input like so:
QWidget *wid = new QWidget();
QVBoxLayout *layout = new QVBoxLayout();
for(int i=0; i<NumberModes; i++)
{
int k = Amplitudes(i,0);
int m = Amplitudes(i,1);
QString ks = QString::number(k);
QString ms = QString::number(m);
QString position = QString::number(i);
QString mode = "A"+ks+ms;
QCheckBox *check = new QCheckBox(mode);
connect(check, SIGNAL(toggled(bool)), &mapper, SLOT(map()));
connect(check, SIGNAL(toggled(bool)), &SelectModes, SLOT(map()));
mapper.setMapping(check,position);
SelectModes.setMapping(check,mode);
layout->addWidget(check);
updateGeometry();
}
wid->setLayout(layout);
ui->scrollArea->setWidget(wid);
The QSignalMapper are then connected to another class that performs some calculations:
connect(&SelectModes, SIGNAL(mapped(QString)), this, SIGNAL(CheckBoxClicked2(QString)));
connect(this, SIGNAL(CheckBoxClicked2(QString)), &Supress2, SLOT(ListenSelectedModes(QString)));
connect(&mapper, SIGNAL(mapped(QString)), this, SIGNAL(CheckBoxClicked(QString)));
connect(this, SIGNAL(CheckBoxClicked(QString)), &Suppress, SLOT(ListenSelectedModes(QString)));
What I need is that the classes only receive signals when the QCheckBox are checked; meaning if you check it once, and then un-check it no signal should be emitted, or received. Not sure what the best approach is.
Any ideas?
With C++11 it's doable simply and without QSignalMapper. Here's an working example.
#include <QWidget>
#include <QCheckBox>
#include <QVBoxLayout>
class QCheckBox;
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
signals:
void checkBoxChecked(QCheckBox *checkBox);
};
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
for (int i = 0; i < 10; ++i) {
QCheckBox *checkBox = new QCheckBox("CheckBox " + QString::number(i + 1));
connect(checkBox, &QCheckBox::toggled, [=](bool checked) {
if (checked)
emit checkBoxChecked(checkBox);
});
layout->addWidget(checkBox);
}
setLayout(layout);
}
The suggestions given by user2672165 are excellent!
If you want to monitor only the check event but not the uncheck event, one way would be to subclass the QCheckBox widget so that it emits a particular signal only when the checkbox is checked (e.g. checkBoxChecked)
Then you connect your signal mapper to the custom signal checkBoxChecked, instead of the standard toggle(bool) signal.
In this way the slot associated to the signal mapper is invoked only when the checkbox is checked and not when it is unchecked.
Here is a simple example
#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSignalMapper>
#include <QCheckBox>
#include <QDebug>
class CheckableCheckBox : public QCheckBox {
Q_OBJECT
public:
CheckableCheckBox(const QString &text, QWidget *parent = 0)
: QCheckBox(text, parent)
{
connect(this, SIGNAL(toggled(bool)),
this, SLOT(verifyCheck(bool)));
}
signals:
void checkBoxChecked();
public slots:
void verifyCheck(bool checked) {
if (checked)
emit checkBoxChecked();
}
};
class Test : public QWidget {
Q_OBJECT
public:
Test(QWidget *parent = 0) : QWidget(parent) {
QSignalMapper *mapper = new QSignalMapper();
QVBoxLayout *layout = new QVBoxLayout();
for (int i = 0; i < 10; i++) {
QString mode = "A" + QString::number(i);
CheckableCheckBox *check = new CheckableCheckBox(mode);
connect(check, SIGNAL(checkBoxChecked()),
mapper, SLOT(map()));
mapper->setMapping(check, QString::number(i));
layout->addWidget(check);
setLayout(layout);
}
connect(mapper, SIGNAL(mapped(QString)),
this, SLOT(CheckBoxClicked(QString)));
}
public slots:
void CheckBoxClicked(const QString &mapping) {
qWarning() << "Checkbox:" << mapping << " is checked";
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test *wid = new Test();
wid->show();
return a.exec();
}
#include "main.moc"
Edit:
If you want to monitor a change in the check status and then notify to some other portions of the code the status of the checkbox (which is probably what you want) you can do something like this... You don't even need a QSignalMapper...
I have implemented a test method testMonitorCheckStatus to show what I mean. Note that you need typedef QList<bool> CheckBoxStatusList; (at least as far as I know) to use QList as an argument to slots and signals.
Edit #2:
The number of checkboxes is set at object creation
Hope this helps
#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSignalMapper>
#include <QCheckBox>
#include <QList>
#include <QDebug>
typedef QList<bool> CheckBoxStatusList;
class Test : public QWidget {
Q_OBJECT
public:
Test(int totalCheckboxes, QWidget *parent = 0) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout();
for (int i = 0; i < totalCheckboxes; i++) {
QString mode = "A" + QString::number(i);
QCheckBox *checkBox = new QCheckBox(mode);
connect(checkBox, SIGNAL(toggled(bool)),
this, SLOT(monitorCheckStatus()));
m_checkBoxList.append(checkBox);
layout->addWidget(checkBox);
}
setLayout(layout);
connect(this, SIGNAL(checkBoxStatusChanged(CheckBoxStatusList)),
this, SLOT(testMonitorCheckStatus(CheckBoxStatusList)));
}
public slots:
void monitorCheckStatus() {
CheckBoxStatusList checkBoxStatus;
for (int i = 0; i < m_checkBoxList.count(); ++i)
checkBoxStatus.append(m_checkBoxList.at(i)->isChecked());
emit checkBoxStatusChanged(checkBoxStatus);
}
void testMonitorCheckStatus(const CheckBoxStatusList &checkBoxStatus) {
for (int i = 0; i < checkBoxStatus.count(); ++i)
qWarning() << "Checkbox:" << i << " is" << (checkBoxStatus.at(i) ? "checked" : "unchecked");
qWarning(" ");
}
signals:
void checkBoxStatusChanged(const CheckBoxStatusList &checkBoxStatus);
private:
QList<QCheckBox *> m_checkBoxList;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test *wid = new Test(10);
wid->show();
return a.exec();
}
#include "main.moc"