Results 1 to 10 of 10

Thread: Trouble with creating custom class

  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

  6. #6
    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

    Sorry I did a poor job of explaining, I had fixed these yesterday and forgot to mention.

    I had done a little debugging yesterday and I forgot to reflect those changes in there

    Basically In main.cpp
    Qt Code:
    1. class Mywidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. MyWidget ( QWidget *parent = 0, const char *name = 0);
    6. };
    7.  
    8. MyWidget::MyWidget ( QWidget *parent, const char *name);
    9. :QWidget (parent, name)
    10. {
    11. QBoxLayout *vbox = new QVBoxLayout(this, 5);
    12.  
    13. QPushButton *quit = new QPushButton ( "&Quit", this, "quit");
    14. quit->setFont( QFont ("Times", 18, QFont::Bold));
    15.  
    16. connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    17.  
    18. ChannelSelected *channelPanel = new ChannelSelected(this);
    19.  
    20. vbox->addWidget(channelPanel);
    21. vbox->addWidget(quit);
    22. }
    To copy to clipboard, switch view to plain text mode 
    everything else should be the same

    for channel selected. cpp file I changed theconstructor to be QWidget and here is the other changes that I didn't mention
    Qt Code:
    1. QBoxLayout *box = new QVBoxLayout(this);
    2. QBoxLayout *hbox = new QHBoxLayout(box);
    3. QBoxLayout *hboxbutt= new QHBoxLayout(box);
    To copy to clipboard, switch view to plain text mode 
    I then added the channels to the hbox and the buttons to hbox butt and the GUI is coming out as expected. The only issue that I have been trying to solve is that the method setChecked() and setUnChecked() hasn't been able to see the QCheckBox::setChecked() method


    Added after 36 minutes:


    Thank you for helping me find a problem in the scope.
    I changed the channelselected.cpp file so that the signals & slots were now only 2 statements.

    connect(allpb, SIGNAL(clicked()), this, SLOT(setChecked()));
    connect(flearpb, SIGNAL(clicked()), this, SLOT(setUnChecked()));


    Added after 10 minutes:


    I put the change in and the program crashes. I am confused why this would happen.
    Last edited by jstippey; 11th January 2011 at 15:02.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Trouble with creating custom class

    Not to sound offensive, but the errors you are making show that you don't really understand how QWidget, the SIGNAL/SLOT mechanism, and putting together a Qt app in general works.

    Qt comes with dozens of good examples and some tutorials, and there are several good books on learning Qt. Why don't you look at some of them? It isn't really the purpose of this form to write your Qt apps for you, and that's what you're basically asking us to do.

  8. #8
    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

    No offense taken, I built this example using the tutorials and the documentation. In particular, this was partly based on tutorial 7 and the custom layouts examples they gave. The reason I asked the question is because I spent a good bit of time trying to debug and was missing something that I still haven't quite figured out

  9. #9
    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

    I am pretty desperate for help right now. I have looked over this code again and again and haven't been able to figure out why it is doing what it is doing. It compiles just fine and the quit button works, however when I push the all or clear button the program crashes. I would be grateful if someone could explain to me what I am doing wrong, I am trying to learn how to create a custom class. Just so everyone knows, I was unable to copy/paste code in so I had to type in, there may be a few typos but they are not part of the code. Your help would be greatly appreciated.

    channelselected.h
    Qt Code:
    1. #ifndef CHANNELSELECTED_H
    2. #define CHANNELSELECTED_H
    3. #include <qvbox.h>
    4. #include <qhbox.h>
    5. #include <qlayout.h>
    6. #include <qobject.h>
    7. class QCheckBox;
    8.  
    9. class ChannelSelected : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. ChannelSelected ( QWidget *parent=0, const char *name=0 );
    14.  
    15. public slots:
    16. void setChecked();
    17. void setUnChecked();
    18.  
    19. signals:
    20. void valueChanged();
    21.  
    22. private;
    23. QPushButton *allpb;
    24. QPushButton *clearpb;
    25. QCheckBox *c0cb;
    26. QCheckBox *c1cb;
    27. QCheckBox *c2cb;
    28. QCheckBox *c3cb;
    29. QCheckBox *c4cb;
    30. QCheckBox *c5cb;
    31. QCheckBox *c6cb;
    32. };
    33. #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. #include <qwidget.h>
    5.  
    6. ChannelSelected::ChannelSelected ( QWidget *parent, const char *name)
    7. : QWidget (parent, name)
    8. {
    9. QCheckBox *c0cb = new QCheckBox ("Channel 0", this, "c0cb");
    10. QCheckBox *c1cb = new QCheckBox ("Channel 1", this, "c1cb");
    11. QCheckBox *c2cb = new QCheckBox ("Channel 2", this, "c2cb");
    12. QCheckBox *c3cb = new QCheckBox ("Channel 3", this, "c3cb");
    13. QCheckBox *c4cb = new QCheckBox ("Channel 4", this, "c4cb");
    14. QCheckBox *c5cb = new QCheckBox ("Channel 5", this, "c5cb");
    15. QCheckBox *c6cb = new QCheckBox ("Channel 6", this, "c6cb");
    16. QPushButton *allpb = new QPushButton("All", this, "allpb");
    17. QPushButton *clearpb = new QPushButton("Clear", this, "clearpb");
    18. QBoxLayout *box = new QVBoxLayout(this);
    19. QHBoxLayout *hbox = new QHBoxLayout(box);
    20. QHboxLayout *hboxbutt= new QHBoxLayout(box);
    21. hbox->addWidget(c0cb);
    22. hbox->addWidget(c1cb);
    23. hbox->addWidget(c2cb);
    24. hbox->addWidget(c3cb);
    25. hbox->addWidget(c4cb);
    26. hbox->addWidget(c5cb);
    27. hbox->addWidget(c6cb);
    28. hboxbutt->addWidget(allpb);
    29. hboxbutt->addWidget(clearpb);
    30. box->addWidget(hbox);
    31. box->addWidget(hboxbutt);
    32. connect (allpb, SIGNAL(clicked()), this, SLOT(setUnChecked()));
    33. connect (clearpb, SIGNAL(clicked()), this, SLOT(setUnChecked()));
    34.  
    35. void ChannelSelected::setChecked()
    36. {
    37. c0cb->setChecked(TRUE);
    38. c1cb->setChecked(TRUE);
    39. c2cb->setChecked(TRUE);
    40. c3cb->setChecked(TRUE);
    41. c4cb->setChecked(TRUE);
    42. c5cb->setChecked(TRUE);
    43. c6cb->setChecked(TRUE);
    44. }
    45.  
    46. void ChannelSelected::setUnChecked()
    47. {
    48. c0cb->setChecked(FALSE);
    49. c1cb->setChecked(FALSE);
    50. c2cb->setChecked(FALSE);
    51. c3cb->setChecked(FALSE);
    52. c4cb->setChecked(FALSE);
    53. c5cb->setChecked(FALSE);
    54. c6cb->setChecked(FALSE);
    55. }
    To copy to clipboard, switch view to plain text mode 

    myWidget.h
    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3. #include <qpushbutton.h>
    4. #include <qwidget.h>
    5.  
    6. class MyWidget : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public :
    11.  
    12. MyWidget ( QWidget *parent = 0, const char *name= 0 );
    13. };
    14. #end if //MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    MyWidget.cpp
    Qt Code:
    1. #include "mywidget.h"
    2. #include "channelselected.h"
    3.  
    4. #include <qapplication.h>
    5. #include <qwidget.h>
    6.  
    7. MyWidget::MyWidget ( QWidget *parent, const char *name)
    8. :QWidget (parent, name)
    9. {
    10. QBoxLayout *vbox = new QVBoxLayout (this, 5);
    11.  
    12. QPushButton *quit = new QPushButton ( "&Quit", this, "quit");
    13. connect ( quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    14.  
    15. ChannelSelected *channelPanel = new ChannelSelected (this);
    16. vbox->addWidget(channelPanel);
    17. vbox->addWidget(quit);
    18. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <qapplication.h>
    2. #include <qpushbutton.h>
    3. #include <qvbox.h>
    4. #include <qlayout.h>
    5. #include <qobject.h>
    6.  
    7. #include "channelselected.h"
    8. #include "mywidget.h"
    9.  
    10. int main ( int argc, char **argv);
    11. {
    12. QApplication a (argc, argv);
    13. MyWidget w;
    14. a.setMainWidget(&w);
    15. w.show();
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    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

    you need to understand basic C++ concepts, such as what is a member.
    You allocate all your buttons in local variables, which die at the end of your constructor, and leak their objects.
    They all need to be members.

    Qt Code:
    1. class ChannelSelected: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public :
    6.  
    7. MyWidget ( QWidget *parent = 0, const char *name= 0 );
    8. protected:
    9. QCheckBox *c0cb;
    10. QCheckBox *c1cb;
    11. QCheckBox *c2cb;
    12. //... an so on
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ChannelSelected::ChannelSelected ( QWidget *parent, const char *name)
    2. : QWidget (parent, name)
    3. {
    4. c0cb = new QCheckBox ("Channel 0", this, "c0cb");
    5. c1cb = new QCheckBox ("Channel 1", this, "c1cb");
    6. //... and so on
    7. }
    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.