Results 1 to 4 of 4

Thread: Problem connecting a PushButton to a Label

  1. #1
    Join Date
    May 2010
    Location
    Brazil
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:

    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include <QHBoxLayout>
    4. #include <QVBoxLayout>
    5. #include <QString>
    6.  
    7. QString infotext = "";
    8. int randgen(int);
    9. int randgen(int x){
    10. x = rand() % 100 + 1;
    11. return x;};
    12.  
    13. class MyWidget : public QWidget
    14. {
    15.  
    16. public:
    17. MyWidget(QWidget *parent = 0);
    18.  
    19.  
    20.  
    21. };
    22. class MyFunctions : public QWidget
    23. {
    24. Q_OBJECT
    25. public:
    26.  
    27. void newClicked();
    28.  
    29. public slots:
    30.  
    31. ;};
    32.  
    33. void MyFunctions::newClicked()
    34. {
    35. QString infotext = "NewGame_Text";
    36. };
    37.  
    38. MyWidget::MyWidget(QWidget *parent)
    39. : QWidget(parent)
    40. {
    41. QWidget *window = new QWidget;
    42. window->setWindowTitle("Guess the number");
    43.  
    44. QLabel *choose = new QLabel("Choose a number");
    45. QSpinBox *spin = new QSpinBox;
    46. spin->setRange(0, 100);
    47. spin->setValue(0);
    48. QLabel *info = new QLabel;
    49. info->setAutoFillBackground(true);
    50. info->setText(infotext);
    51. QPushButton *newGame = new QPushButton("New Game");
    52. QPushButton *guess = new QPushButton("Guess");
    53.  
    54. QHBoxLayout *input = new QHBoxLayout;
    55. input->addWidget(choose);
    56. input->addWidget(spin);
    57. QVBoxLayout *leftLayout = new QVBoxLayout;
    58. leftLayout->addLayout(input);
    59. leftLayout->addWidget(info);
    60. QVBoxLayout *rightLayout = new QVBoxLayout;
    61. rightLayout->addWidget(newGame);
    62. rightLayout->addWidget(guess);
    63. QHBoxLayout *layout = new QHBoxLayout;
    64. layout->addLayout(leftLayout);
    65. layout->addLayout(rightLayout);
    66. window->setLayout(layout);
    67.  
    68.  
    69.  
    70. // info->setText("info here");
    71.  
    72. connect(newGame, SIGNAL(clicked()), MyFunctions, SLOT(newClicked()));
    73.  
    74. window->show();
    75. };
    76.  
    77.  
    78. /*void MyWidget::newClicked()
    79. {
    80. QString infotext = "NewGameText";
    81. };*/
    82.  
    83.  
    84.  
    85. int main(int argc, char *argv[])
    86. {
    87. QApplication app(argc, argv);
    88. MyWidget widget;
    89.  
    90. return app.exec();
    91. }
    To copy to clipboard, switch view to plain text mode 

    Compiler's Output:

    Qt Code:
    1. guess.cpp: In constructor ‘MyWidget::MyWidget(QWidget*):
    2. guess.cpp:72: error: expected primary-expression before ‘,’ token
    3. make: *** [guess.o] Error 1
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default 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.

  3. #3
    Join Date
    May 2010
    Location
    Brazil
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  4. #4
    Join Date
    May 2010
    Location
    Brazil
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

Similar Threads

  1. label - pixmap - problem
    By qwrhiobasdbgghoasdf in forum Newbie
    Replies: 5
    Last Post: 16th September 2010, 01:29
  2. Problem with setFont for a PushButton
    By arunvv in forum Newbie
    Replies: 12
    Last Post: 8th April 2008, 00:35
  3. connecting image to a pushbutton
    By sudheer in forum Qt Tools
    Replies: 2
    Last Post: 4th December 2007, 10:23
  4. Pushbutton sizing problem on Mac OS
    By will49 in forum Qt Programming
    Replies: 5
    Last Post: 3rd October 2007, 22:26
  5. PushButton height problem
    By impeteperry in forum Qt Tools
    Replies: 4
    Last Post: 28th January 2007, 21:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.