Reputation: 11
Ive gone through console applications and i thought about doing something different than that, to move on and do something more ambitious. My friend have told me about ide Qt. It was going nice, till i reached a wall, which is really stoping me from developing my first app in all the way new IDE for me. Previously i have used Visual Studio, and due to massive amount of walktroughs along the internet i managed to often find answers myself, but this time i give up! So this is the first time i ask for help. My app is supposed to be a simple calculator. The problem is that i cant make the QEditLine object to show numebrs that are represented by buttons, it never changes and shows 0. It works if i use design mode, and do it all the same, and the definition of dgtClicked() function is almost the same in the example provided by QT. Ill be more than happy for any suggestions and help. This is really annoying and i dont really like leaving unnanswered questions like this.
#include "klk.h"
#include "ui_klk.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)//,
// ui(new Ui::MainWindow)
{
//ui->setupUi(this);
dgtButton[0] = new QPushButton("&0");
dgtButton[1] = new QPushButton("&1");
dgtButton[2] = new QPushButton("&2");
dgtButton[3] = new QPushButton("&3");
dgtButton[4] = new QPushButton("&4");
dgtButton[5] = new QPushButton("&5");
dgtButton[6] = new QPushButton("&6");
dgtButton[7] = new QPushButton("&7");
dgtButton[8] = new QPushButton("&8");
dgtButton[9] = new QPushButton("&9");
for(int i =0;i<10;i++) {
connect(dgtButton[i], SIGNAL(clicked()),this,SLOT(dgtClicked()));
}
display = new QLineEdit("0");
display->setAlignment(Qt::AlignRight);
display->setMaxLength(15);
display->setReadOnly(true);
// centralWidget = new QWidget(this);
//this->setCentralWidget( centralWidget );
// layout = new QGridLayout( centralWidget );
layout = new QGridLayout;
layout->setSizeConstraint(QLayout::SetFixedSize);
layout->addWidget(display,0,0,1,3);
layout->addWidget(dgtButton[1],2,0);
layout->addWidget(dgtButton[2],2,1);
layout->addWidget(dgtButton[3],2,2);
layout->addWidget(dgtButton[4],3,0);
layout->addWidget(dgtButton[5],3,1);
layout->addWidget(dgtButton[6],3,2);
layout->addWidget(dgtButton[7],4,0);
layout->addWidget(dgtButton[8],4,1);
layout->addWidget(dgtButton[9],4,2);
setLayout(layout);
}
void MainWindow::dgtClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
dgt = clickedButton->text().toInt();
this->display->setText(QString::number(dgt));
//display->displayText();
// display->show();
}
MainWindow::~MainWindow()
{
//delete ui;
}
#ifndef KLK_H
#define KLK_H
#include <QMainWindow>
#include <QPushButton>
#include <QWidget>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <string>
namespace Ui {
class MainWindow;
}
class MainWindow : public QWidget
{
Q_OBJECT
QPushButton *dgtButton[10];
QPushButton *addButton,*subButton;
QGridLayout *layout;
QWidget *centralWidget;
QLineEdit *display;
int dgt;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
// Ui::MainWindow *ui;
private slots:
void dgtClicked();
};
#endif // KLK_H
#-------------------------------------------------
#
# Project created by QtCreator 2014-04-30T15:37:36
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = klk
TEMPLATE = app
SOURCES += main.cpp\
klk.cpp
HEADERS += klk.h
FORMS += klk.ui
Upvotes: 0
Views: 202
Reputation: 53173
You have at least two issues:
You are trying to convert non-numbers ("&1, "&2, etc) to integer. That is guaranteed to fail.
You are not checking the QString::toInt()
conversion for errors.
You could have caught the former easier if you had written the conversion operation like this:
bool ok;
dgt = clickedButton->text().toInt(&ok);
if (!ok)
qDebug() << "Failed to convert to integer:" << clickedButton->text();
The documentation is also clear about it:
int QString::toInt(bool * ok = 0, int base = 10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
Having that realized, changing the following lines will fix your code:
for(int i =0;i<10;i++) {
dgtButton[i] = new QPushButton(QString::number(i));
connect(dgtButton[i], SIGNAL(clicked()),this,SLOT(dgtClicked()));
}
As you can see, I fixed two further issues in your code:
Reduce the manual typing by sanitizing the loop.
I used the following function to actually get the QString
value out of the integer which is the loop counter in this particular case.
Just in case, for completeness, this line is needless in your qmake project file even though QtCreator generates it improperly:
QT += core gui
The core and gui modules are added by default so you can safely remove that.
Upvotes: 2