Problem connecting a PushButton to a Label
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:
Code:
#include <QtGui>
#include <QtCore>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QString>
int randgen(int);
int randgen(int x){
x = rand() % 100 + 1;
return x;};
{
public:
};
{
Q_OBJECT
public:
void newClicked();
public slots:
;};
void MyFunctions::newClicked()
{
};
MyWidget
::MyWidget(QWidget *parent
) {
window->setWindowTitle("Guess the number");
spin->setRange(0, 100);
spin->setValue(0);
info->setAutoFillBackground(true);
info->setText(infotext);
input->addWidget(choose);
input->addWidget(spin);
leftLayout->addLayout(input);
leftLayout->addWidget(info);
rightLayout->addWidget(newGame);
rightLayout->addWidget(guess);
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[])
{
MyWidget widget;
return app.exec();
}
Compiler's Output:
Code:
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.
Re: Problem connecting a PushButton to a Label
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.
Re: Problem connecting a PushButton to a Label
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.
Re: Problem connecting a PushButton to a Label
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.