[Qt] Primjer iz knjige (muke)

C, C++, Java, PHP, Ruby...

Moderator/ica: Moderatori/ce

Pravila foruma
U naslovu teme unutar uglatih zagrada navesti o kojem jeziku je riječ. Primjer: [Java]
Odgovori
Nele
Postovi: 248
Pridružen/a: 07 lip 2009, 12:55
Spol: M

[Qt] Primjer iz knjige (muke)

Post Postao/la Nele »

Poceo sam citati knjigu "The art of building Qt applications" i zapeo na drugom poglavlju.
U cemu je greska?
ByteConverterDialog.cpp

Kod: Označi sve

#include "ByteConverterDialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLAyout>
#include <QGridLayout>
#include <QIntValidator>
#include <QRegExpValidator>
ByteConverterDialog::ByteConverterDialog()
{
QVBoxLayout* mainLayout=new QVBoxLayout(this);
QGridLayout* editLayout=new QGridLayout;
QHBoxLayout* buttonLayout=new QHBoxLayout;
mainLayout->addLayout(editLayout);
mainLayout->addStretch();
mainLayout->addLayout(buttonLayout);
QLabel* decLabel=new QLabel(tr("Decimal"));
QLabel* hexLabel=new QLabel(tr("Hex"));
QLabel* binLabel=new QLabel(tr("Binary"));
decEdit=new QLineEdit;
hexEdit=new QLineEdit;
binEdit=new QLineEdit;
editLayout->addWidget(decLabel,0,0);
editLayout->addWidget(decEdit,0,1);
editLayout->addWidget(hexLabel,1,0);
editLayout->addWidget(hexEdit,1,1);
editLayout->addWidget(binLabel,2,0);
editLayout->addWidget(binEdit,2,1);
QPushButton* exitButton=new QPushButton(tr("Quit"));
buttonLayout->addStretch();
buttonLayout->addWidget(exitButton);
exitButton->setDefault(true);
QIntValidator* decValidator=new QIntValidator(0,255,decEdit);
decEdit->setValidator(decValidator);
QRegExpValidator* hexValidator=new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,2}"),hexEdit);
hexEdit->setValidator(hexValidator);
QRegExpValidator* binValidator=new QRegExpValidator(QRegExp("[01]{1,8}"),binEdit);
binEdit->setValidator(binValidator);
setWindowTitle(tr("Byte Converter"));
connect(exitButton,SIGNAL(clicked()),this,SLOT(accept()));
void ByteConverterDialog::decChanged(const QString& newValue) 
{
bool ok;
int num=newValue.toInt(&ok);
if (ok) {
	hexEdit -> setText(QString::number(num,16));
	binEdit->setText(QString::number(num,2));
} else {
	hexEdit->setText("");
	binEdit->setText("");
}
}
void ByteConverterDialog::hexChanged(const QString& newValue)
{
bool ok;
int num=newValue.toInt(&ok);
if (ok) {
	decEdit -> setText(QString::number(num));
	binEdit->setText(QString::number(num,2));
} else {
	decEdit->setText("");
	binEdit->setText("");
}
}
void ByteConverterDialog::binChanged(const QString& newValue)
{
bool ok;
int num=newValue.toInt(&ok);
	if (ok) {
	decEdit -> setText(QString::number(num));
	hexEdit->setText(QString::number(num,16));
} else {
	decEdit->setText("");
	hexEdit->setText("");
}
}
connect(decEdit,SIGNAL(textChanged(const QString&)),this,SLOT(decChanged(const QString&)));
connect(hexEdit,SIGNAL(textChanged(const QString&)),this,SLOT(hexChanged(const QString&)));
connect(binEdit,SIGNAL(textChanged(const QString&)),this,SLOT(binChanged(const QString&)));
}

ByteConverterDialog.h

Kod: Označi sve

#ifndef BYTECONVERTERDIALOG_H
#define BYTECONVERTERDIALOG_H
#include <QDialog>
class QLineEdit;
class ByteConverterDialog:public QDialog
{
Q_OBJECT
public:
ByteConverterDialog();
private:
QLineEdit* decEdit;
QLineEdit* hexEdit;
QLineEdit* binEdit;
private slots:
void decChanged(const QString&);
void hexChanged(const QString&);
void binChanged(const QString&);
};
#endif
main.cpp

Kod: Označi sve

#include <QApplication>
#include "ByteConverterDialog.h"
int main(int argc,char * argv[])
{
QApplication a(argc,argv);
ByteConverterDialog bc;
bc.setAttribute(Qt::WA_QuitOnClose);
bc.show();
return a.exec();
}
Javlja mi gresku u fajlu ByteConverterDialog.cpp na liniji void ByteConverterDialog::decChanged(const QString& newValue)
schmrz
Postovi: 11
Pridružen/a: 18 vel 2009, 23:46

Re: [Qt] Primjer iz knjige (muke)

Post Postao/la schmrz »

Vjerujem da bi ti neko mogao pomoci kada bi postavio i gresku koju dobivas.
Nele
Postovi: 248
Pridružen/a: 07 lip 2009, 12:55
Spol: M

Re: [Qt] Primjer iz knjige (muke)

Post Postao/la Nele »

Kod: Označi sve

ByteConverterDialog.cpp: In constructor `ByteConverterDialog::ByteConverterDialo
g()':
ByteConverterDialog.cpp:43: error: a function-definition is not allowed here bef
ore '{' token
ByteConverterDialog.cpp:43: error: expected `,' or `;' before '{' token
ByteConverterDialog.cpp:55: error: a function-definition is not allowed here bef
ore '{' token
ByteConverterDialog.cpp:55: error: expected `,' or `;' before '{' token
ByteConverterDialog.cpp:67: error: a function-definition is not allowed here bef
ore '{' token
ByteConverterDialog.cpp:67: error: expected `,' or `;' before '{' token
mingw32-make[1]: *** [debug/ByteConverterDialog.o] Error 1
mingw32-make[1]: Leaving directory `C:/Users/Nedim/Desktop/ByteConverterDialog'
mingw32-make: *** [debug] Error 2
Avatar
stefan
Moderator
Postovi: 4366
Pridružen/a: 28 sij 2009, 18:46
Spol: M
OS: openSUSE Leap KDE

Re: [Qt] Primjer iz knjige (muke)

Post Postao/la stefan »

Kao što ti i error kaže - ti si definirao funkciju (u biti više njih) unutar konstruktora ByteConverterDialog

Umjesto:

Kod: Označi sve

ByteConverterDialog::ByteConverterDialog()
{
    QVBoxLayout* mainLayout=new QVBoxLayout(this);
    QGridLayout* editLayout=new QGridLayout;
    ...
    setWindowTitle(tr("Byte Converter"));
    connect(exitButton,SIGNAL(clicked()),this,SLOT(accept()));
    void ByteConverterDialog::decChanged(const QString& newValue)
    {
    bool ok;
    int num=newValue.toInt(&ok);
    if (ok) {
       hexEdit -> setText(QString::number(num,16));
       binEdit->setText(QString::number(num,2));
    } else {
       hexEdit->setText("");
       binEdit->setText("");
    }
    }
    void ByteConverterDialog::hexChanged(const QString& newValue)
    {
    ...
    }
    void ByteConverterDialog::binChanged(const QString& newValue)
    {
    ...
    }
    connect(decEdit,SIGNAL(textChanged(const QString&)),this,SLOT(decChanged(const QString&)));
    connect(hexEdit,SIGNAL(textChanged(const QString&)),this,SLOT(hexChanged(const QString&)));
    connect(binEdit,SIGNAL(textChanged(const QString&)),this,SLOT(binChanged(const QString&)));
}
napravi ovako:

Kod: Označi sve

ByteConverterDialog::ByteConverterDialog()
{
    QVBoxLayout* mainLayout=new QVBoxLayout(this);
    QGridLayout* editLayout=new QGridLayout;
    ...
    setWindowTitle(tr("Byte Converter"));
    connect(exitButton,SIGNAL(clicked()),this,SLOT(accept()));
    connect(decEdit,SIGNAL(textChanged(const QString&)),this,SLOT(decChanged(const QString&)));
    connect(hexEdit,SIGNAL(textChanged(const QString&)),this,SLOT(hexChanged(const QString&)));
    connect(binEdit,SIGNAL(textChanged(const QString&)),this,SLOT(binChanged(const QString&)));
}
void ByteConverterDialog::decChanged(const QString& newValue)
{
    bool ok;
    int num=newValue.toInt(&ok);
    if (ok) {
       hexEdit -> setText(QString::number(num,16));
       binEdit->setText(QString::number(num,2));
    } else {
       hexEdit->setText("");
       binEdit->setText("");
    }
}

void ByteConverterDialog::hexChanged(const QString& newValue)
{
    ...
}

void ByteConverterDialog::binChanged(const QString& newValue)
{
    ...
}
da skratim:

Kod: Označi sve

ByteConverterDialog::ByteConverterDialog()
{
}

void ByteConverterDialog::hexChanged(const QString& newValue)
{
    ...
}

void ByteConverterDialog::binChanged(const QString& newValue)
{
    ...
}
funkcije ti moraju biti definirane jedna nakon druge, a nikako jedna u drugoj.
Like some other animals, the gecko can perform a neat trick when threatened by a predator: it can amputate its own tail. The dropped tail serves to distract the predator, and by losing it, the lizard can run faster.
Nele
Postovi: 248
Pridružen/a: 07 lip 2009, 12:55
Spol: M

Re: [Qt] Primjer iz knjige (muke)

Post Postao/la Nele »

Hvala to je to.Ja tek sad primjetio kakvu sam glupost napisao :oops:
Odgovori