Results 1 to 5 of 5

Thread: What's wrong with my code?

  1. #1
    Join Date
    Jul 2011
    Location
    China Hunan province
    Posts
    7
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default What's wrong with my code?

    Code:
    **************************
    lcdrange.h:
    Qt Code:
    1. #ifndef LCDRANGE_H
    2. #define LCDRANGE_H
    3.  
    4. #endif // LCDRANGE_H
    5. #include<QWidget.h>
    6. #include<QSlider.h>
    7. #include<QLcdnumber.h>
    8.  
    9. class Lcdrange: public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. Lcdrange();
    14. int value();
    15. private:
    16. QSlider* slider;
    17. QLCDNumber* lcd;
    18. public slots:
    19. void setValue(int);
    20. signals:
    21. void valueChanged(int);
    22. };
    To copy to clipboard, switch view to plain text mode 

    ***************************
    lcdrange.cpp:

    Qt Code:
    1. #include"lcdrange.h"
    2.  
    3. Lcdrange::Lcdrange()
    4. {
    5. slider=new QSlider(Qt::Horizontal,this);
    6. slider->setRange(0,99);
    7. slider->setGeometry(0,0,200,50);
    8.  
    9. lcd=new QLCDNumber(2,this);
    10. lcd->setGeometry(0,50,200,50);
    11.  
    12. connect(slider,SIGNAL(valueChanged(int)),lcd,SLOT(display(int)));
    13. connect(slider,SIGNAL(valueChanged(int)),SIGNAL(valueChanged(int)));
    14. }
    15.  
    16. int Lcdrange::value()
    17. {
    18. return slider->value();
    19. }
    20. void Lcdrange::setValue(int value)
    21. {
    22. return slider->setValue(value);
    23. }
    To copy to clipboard, switch view to plain text mode 


    ************************************
    main.cpp:
    Qt Code:
    1. #include<qapplication.h>
    2. #include<qpushbutton.h>
    3. #include"lcdrange.h"
    4.  
    5. class MyWindow: public Lcdrange
    6. {
    7. public:
    8. MyWindow();
    9. private:
    10. QPushButton* button;
    11. };
    12.  
    13. MyWindow::MyWindow()
    14. {
    15. setGeometry(200,150,200,250);
    16.  
    17. button=new QPushButton("Quit!",this);
    18. button->setGeometry(100,210,90,40);
    19.  
    20. Lcdrange* range=new Lcdrange;
    21. range->setGeometry(0,0,200,100);
    22.  
    23. Lcdrange* ranges=new Lcdrange;
    24. ranges->setGeometry(0,100,200,100);
    25.  
    26. connect(range,SIGNAL(valueChanged(int)),ranges,SLOT(setValue(int)));
    27. connect(button,SIGNAL(clicked()),qApp,SLOT(quit()));
    28. }
    29.  
    30. int main(int argc,char** argv)
    31. {
    32. QApplication a(argc,argv);
    33.  
    34. MyWindow mw;
    35.  
    36. mw.show();
    37. return a.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 9th August 2011 at 16:20.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: What's wrong with my code?

    Qt Code:
    1. #endif // LCDRANGE_H
    To copy to clipboard, switch view to plain text mode 
    should be at the end of the .h file, preventing including it if it has already been included.

    And MyWindow class needs to be defined in a .h file, so it can include the Q_OBJECT macro, which it needs for signals & slots to work.

    Other than that, there must be a reason to think there is something wrong. Usually the reason for that is that the code does not work. If that is the case, you should describe what it does and what it does wrong.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: What's wrong with my code?

    Should we search or would you be so kind to tell us, what error do you get? Also please note the [code] tags.

  4. The following user says thank you to Lykurg for this useful post:

    Dave2011 (12th August 2011)

  5. #4
    Join Date
    Jul 2011
    Location
    China Hunan province
    Posts
    7
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What's wrong with my code?

    I am a beginner here,I am so sorry I didn't know how to note [code] tags.I designed the MyWindow class have two lcdnumber and two slider,but it just showed one lcdnumber and one slider.I don't know what'wrong with it.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What's wrong with my code?

    Please learn to use layouts in your programs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. What is wrong with this code?
    By Jeffb in forum Newbie
    Replies: 6
    Last Post: 18th February 2011, 07:35
  2. Qt Creator Green lines under everything suggesting all my code is wrong
    By Sydius in forum Qt Tools
    Replies: 1
    Last Post: 21st June 2010, 04:57
  3. What is wrong with my code?
    By Dante in forum Qt Programming
    Replies: 27
    Last Post: 6th April 2009, 07:56
  4. Pasting code from code tag in emacs
    By Gopala Krishna in forum General Discussion
    Replies: 0
    Last Post: 16th February 2007, 05:47

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.