PDA

View Full Version : Problem connecting a PushButton to a Label



Aya
11th May 2010, 05:48
Hello, i'm a total noob to QT, but i'm really into learning it.
Been trying to solve this by myself for some time now, and i think i'm close now, but still, i'm at a complete loss here.
If someone could help me out, the idea is making a 'guess the number' kind of program, i managed to get the layout done and all, i'm now trying to add some functionality. The first thing i'm trying to implement is that the NewGame button make the infoLabel change it's value.
By now, i managed to understand how to do it, and implemented a function to do so, but when i compile, the compiler won't recognize the class i created to store the function calls.

I'll post here the code, followed by the compiler's output:


#include <QtGui>
#include <QtCore>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QString>

QString infotext = "";
int randgen(int);
int randgen(int x){
x = rand() % 100 + 1;
return x;};

class MyWidget : public QWidget
{

public:
MyWidget(QWidget *parent = 0);



};
class MyFunctions : public QWidget
{
Q_OBJECT
public:

void newClicked();

public slots:

;};

void MyFunctions::newClicked()
{
QString infotext = "NewGame_Text";
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QWidget *window = new QWidget;
window->setWindowTitle("Guess the number");

QLabel *choose = new QLabel("Choose a number");
QSpinBox *spin = new QSpinBox;
spin->setRange(0, 100);
spin->setValue(0);
QLabel *info = new QLabel;
info->setAutoFillBackground(true);
info->setText(infotext);
QPushButton *newGame = new QPushButton("New Game");
QPushButton *guess = new QPushButton("Guess");

QHBoxLayout *input = new QHBoxLayout;
input->addWidget(choose);
input->addWidget(spin);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(input);
leftLayout->addWidget(info);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(newGame);
rightLayout->addWidget(guess);
QHBoxLayout *layout = new QHBoxLayout;
layout->addLayout(leftLayout);
layout->addLayout(rightLayout);
window->setLayout(layout);



// info->setText("info here");

connect(newGame, SIGNAL(clicked()), MyFunctions, SLOT(newClicked()));

window->show();
};


/*void MyWidget::newClicked()
{
QString infotext = "NewGameText";
};*/



int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;

return app.exec();
}

Compiler's Output:


guess.cpp: In constructor ‘MyWidget::MyWidget(QWidget*)’:
guess.cpp:72: error: expected primary-expression before ‘,’ token
make: *** [guess.o] Error 1


Don't know what's wrong.
Thanks in advance.

tbscope
11th May 2010, 06:04
There are a couple of things wrong:

1. Seed your random number generator. Look into srand()

2. You need to create an object before you can use it. The error you see is because you try to access a non existing object MyFunctions. MyFunctions is a class (a "description" of an object). You need to first create an object based on that class to be able to use it. Example: in your MyWidget constructor: MyFunctions *myFunctions = new MyFunctions;
Then use myFunctions when you connect the signal of the button to the slot in your MyFunctions class.

3. A slot needs to be defined by public|protected|private slots. You did not add the newClicked() fuinction to public slots.

Aya
12th May 2010, 02:11
Thanks a lot, after reading your answer i realized i didn't have a good knowledge about constructing and implementing a class. I've been studying a little more and i think i might be able to do it soon.

Aya
12th May 2010, 23:53
Just to let you know, managed to do it. You actually helped a lot putting me in the right direction, thanks again.

PS: Didn't find an EDIT button.