Results 1 to 10 of 10

Thread: Trouble with creating custom class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Trouble with creating custom class

    I have been trying to write a simple class file and can't figure out what I am doing wrong. It is one .h file, one .cpp file, and the main file. Any help would be greatly appreciated. It appears to not like my main file when compiling. I am getting errors on 11,13,16,17,19. In addition, I am getting errors on my QPushButton callout for "quit" and my QVBoxLayout in the main.cc

    channelselected.h file

    Qt Code:
    1. #ifndef CHANNELSELECTED_H
    2. #define CHANNELSELECTED_H
    3.  
    4. #include <qvbox.h>
    5. #include <qhbox.h>
    6. #include <qlayout.h>
    7. #include <qobject.h>
    8.  
    9. class QCheckBox;
    10.  
    11. class ChannelSelected : public QVBoxLayout
    12. {
    13. Q_OBJECT
    14. public:
    15. ChannelSelected ( QWidget *parent=0, const char *name=0 );
    16. bool isChecked() const;
    17.  
    18. public slots:
    19. void setChecked();
    20. void setUnChecked();
    21.  
    22. signals:
    23. void valueChanged();
    24.  
    25. private;
    26. QPushButton *allpb;
    27. QPushButton *clearpb;
    28. QCheckBox *c0cb;
    29. QCheckBox *c1cb;
    30. QCheckBox *c2cb;
    31. QCheckBox *c3cb;
    32. QCheckBox *c4cb;
    33. QCheckBox *c5cb;
    34. QCheckBox *c6cb;
    35. };
    36. #endif // CHANNELSELECTED_H
    To copy to clipboard, switch view to plain text mode 

    channelselected.cpp
    Qt Code:
    1. #include "channelselected.h"
    2. #include <qcheckbox.h>
    3. #include <qpushbutton.h>
    4.  
    5. ChannelSelected::ChannelSelected ( QWidget *parent, const char *name)
    6. : QVBoxLayout (parent, name);
    7. {
    8. QCheckBox *c0cb = new QCheckBox ("Channel 0", this, "c0cb");
    9. QCheckBox *c1cb = new QCheckBox ("Channel 1", this, "c1cb");
    10. QCheckBox *c2cb = new QCheckBox ("Channel 2", this, "c2cb");
    11. QCheckBox *c3cb = new QCheckBox ("Channel 3", this, "c3cb");
    12. QCheckBox *c4cb = new QCheckBox ("Channel 4", this, "c4cb");
    13. QCheckBox *c5cb = new QCheckBox ("Channel 5", this, "c5cb");
    14. QCheckBox *c6cb = new QCheckBox ("Channel 6", this, "c6cb");
    15. QPushButton *allpb = new QPushButton("All", this, "allpb");
    16. QPushButton *clearpb = new QPushButton("Clear", this, "clearpb);
    17. QVBoxLayout *box = new QVBoxLayout(this);
    18. QHBox *hbox = new QHBox(box);
    19. QHbox *hboxbutt= new QHBox(box);
    20. hbox->addWidget(c0cb);
    21. hbox->addWidget(c1cb);
    22. hbox->addWidget(c2cb);
    23. hbox->addWidget(c3cb);
    24. hbox->addWidget(c4cb);
    25. hbox->addWidget(c5cb);
    26. hbox->addWidget(c6cb);
    27. hboxbutt->addWidget(allpb);
    28. hboxbutt->addWidget(clearpb);
    29. box->addWidget(hbox);
    30. box->addWidget(hboxbutt);
    31.  
    32. connect (allpb, SIGNAL(clicked()), c0cb, SLOT(setChecked()));
    33. connect (allpb, SIGNAL(clicked()), c1cb, SLOT(setChecked()));
    34. connect (allpb, SIGNAL(clicked()), c2cb, SLOT(setChecked()));
    35. connect (allpb, SIGNAL(clicked()), c3cb, SLOT(setChecked()));
    36. connect (allpb, SIGNAL(clicked()), c4cb, SLOT(setChecked()));
    37. connect (allpb, SIGNAL(clicked()), c5cb, SLOT(setChecked()));
    38. connect (allpb, SIGNAL(clicked()), c6cb, SLOT(setChecked()));
    39.  
    40. connect(clearpb, SIGNAL(clicked()), c0cb, SLOT(setChecked()));
    41. connect(clearpb, SIGNAL(clicked()), c1cb, SLOT(setChecked()));
    42. connect(clearpb, SIGNAL(clicked()), c2cb, SLOT(setChecked()));
    43. connect(clearpb, SIGNAL(clicked()), c3cb, SLOT(setChecked()));
    44. connect(clearpb, SIGNAL(clicked()), c4cb, SLOT(setChecked()));
    45. connect(clearpb, SIGNAL(clicked()), c5cb, SLOT(setChecked()));
    46. connect(clearpb, SIGNAL(clicked()), c6cb, SLOT(setChecked()));
    47. }
    48.  
    49. bool ChannelSelected::isChecked() const
    50. {
    51. return;
    52. }
    53.  
    54. void ChannelSelected::setChecked()
    55. {
    56. c0cb->setChecked(TRUE);
    57. c1cb->setChecked(TRUE);
    58. c2cb->setChecked(TRUE);
    59. c3cb->setChecked(TRUE);
    60. c4cb->setChecked(TRUE);
    61. c5cb->setChecked(TRUE);
    62. c6cb->setChecked(TRUE);
    63. }
    64.  
    65. void ChannelSelected::setUnChecked()
    66. {
    67. c0cb->setChecked(FALSE);
    68. c1cb->setChecked(FALSE);
    69. c2cb->setChecked(FALSE);
    70. c3cb->setChecked(FALSE);
    71. c4cb->setChecked(FALSE);
    72. c5cb->setChecked(FALSE);
    73. c6cb->setChecked(FALSE);
    74. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp file

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qpushbutton.h>
    3. #include <qfont.h>
    4. #include <qvbox.h>
    5. #include <qlayout.h>
    6. #include <qobject.h>
    7.  
    8. #include "channelselected.h"
    9.  
    10. class MyWidget : public QVBoxLayout
    11. {
    12. public:
    13. MyWidget( QWidget *parent=0, const char *name=0);
    14. };
    15.  
    16. MyWidget::MyWidget ( QWidget *parent, int, int, const char *name)
    17. :QVBoxLayout(parent, int, int, name)
    18. {
    19. QVBoxLayout *vbox = new QVBoxLayout ( this, 10, 10, "vbox");
    20.  
    21. QPushButton *quit = new QPushButton ("&Quit", vbox, "quit" );
    22. quit->setFont ( QFont("Times", 18, QFont::Bold));
    23.  
    24. connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
    25. ChannelSelected* channelPanel = new ChannelSelected (vbox, 10, 10, "channelPanel")
    26. vbox->addWidget(channelPanel);
    27. vbox->addWidget(quit);
    28. }
    29.  
    30. int main ( int argc, char **argv)
    31. {
    32. QApplication a (argc, argv);
    33. MyWidget w;
    34. a.setMainWidget(&w);
    35. w.show();
    36. return a.exec();
    37. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Trouble with creating custom class

    The first issue i see is that you are trying to create QWidgets but you are inheriting from a layout, this code:
    Qt Code:
    1. class MyWidget : public QVBoxLayout
    To copy to clipboard, switch view to plain text mode 
    should be something like:
    Qt Code:
    1. class MyWidget : public QWidget
    To copy to clipboard, switch view to plain text mode 
    Also don't forget to modify the parameters passing for constructor to QWidget (not whateverLayout)
    Both your classes have the above issue.

    And the second issue is that you create a QObject derived class in main.cpp, so my sugestion is to create for MyWidget class a MyWidget.h and MyWidget.cpp
    Else you need to include main.moc in the end of the main.cpp.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with creating custom class

    I am getting errors on my QPushButton callout for "quit" and my QVBoxLayout in the main.cc
    Also, you have a typo (the error should point to this line)
    Qt Code:
    1. ...
    2. QCheckBox *c5cb = new QCheckBox ("Channel 5", this, "c5cb");
    3. QCheckBox *c6cb = new QCheckBox ("Channel 6", this, "c6cb");
    4. QPushButton *allpb = new QPushButton("All", this, "allpb");
    5. QPushButton *clearpb = new QPushButton("Clear", this, "clearpb);//<<--Typo
    6. QVBoxLayout *box = new QVBoxLayout(this);
    7. QHBox *hbox = new QHBox(box);
    8. QHbox *hboxbutt= new QHBox(box);
    9. ...
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with creating custom class

    Thank you guys for helping me with this issue. So I went back and changed the following lines

    h file

    class ChannelSelected : public QWidget


    cpp file

    ChannelSelected::ChannelSelected( QWidget *parent, const char *name)
    :QWidget(parent, name)

    checked the clear pb error, but I accidentally changed it when I copied it over.

    Does this take care of issue 1 Zlatimir?

    And I wanted to ask about issue number 2. Is this because I nested the quit button in main.cpp with a connect to the app. I just want to make sure that I understand exactly why. When I ran the code with the changes I made, it compiles and it shows that the signal is sent from the allpb/clearpb to the checkboxes, however is says that I have no such slot- QCheckBox :: setChecked and no such slot- QCheckBox::setUnChecked and the checkboxes don't get set/unset.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Trouble with creating custom class

    There you want to call setCghecked() of your class (ChannelSelected:: ) i guess?
    Then the code should be:
    Qt Code:
    1. connect (allpb, SIGNAL(clicked()), this, SLOT(setChecked()));
    To copy to clipboard, switch view to plain text mode 

    And you have some other issues in the other class, like this:
    Qt Code:
    1. QPushButton *quit = new QPushButton ("&Quit", vbox, "quit" );
    To copy to clipboard, switch view to plain text mode 
    See the constructors of QPushButton

    And you are trying to pass a layout as a parent (in the same QPushButton code above, vbox is a layout)
    The parent must be a QWidget not a Layout, the layout can have a QWidget parent and re-parents all the widgets added to it to it's parent, but the layout can't be a parent for some widgets.
    Solution is: 1) create a layout (if you set a parent you don't need step 3 ), 2) then use addWidget method to add widgets and addLayout for other nested layouts and 3) call setLayout to set a parent for all.

    And i don't understand why you keep passing around NULL char pointer

Similar Threads

  1. Stuck creating << and >> operators for my custom class.
    By agerlach in forum Qt Programming
    Replies: 12
    Last Post: 3rd January 2011, 21:44
  2. Custom Plugin Trouble
    By stevey in forum Qt Programming
    Replies: 5
    Last Post: 3rd November 2009, 17:08
  3. Trouble with playing custom format in phonon
    By lilesh in forum Qt Programming
    Replies: 0
    Last Post: 3rd October 2009, 12:03
  4. Trouble using custom datatype (QMetaType) in QtTest
    By perden in forum Qt Programming
    Replies: 6
    Last Post: 2nd September 2009, 15:26
  5. Qwt/custom widget trouble?
    By Nick2463 in forum Qwt
    Replies: 6
    Last Post: 17th June 2008, 02:48

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
  •  
Qt is a trademark of The Qt Company.