Results 1 to 9 of 9

Thread: Custom Widget - First Steps

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Custom Widget - First Steps

    This should get you started:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "TabWidget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. TabWidget w;
    8. w.show();
    9. return a.exec();
    10. }
    11.  
    12. #include <QList>
    13. #include <QWidget>
    14.  
    15. class SubMaster;
    16.  
    17. class TabWidget : public QWidget
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. TabWidget(QWidget * parent = 0);
    23. virtual ~TabWidget();
    24.  
    25. signals:
    26. void buttonOneClicked(int submaster);
    27. void buttonTwoClicked(int submaster);
    28. void buttonThreeClicked(int submaster);
    29.  
    30. void sliderValueChanged(int submaster, int value);
    31. void spinboxValueChanged(int submaster, int value);
    32.  
    33. public slots:
    34. void handleSliderValueChange(int submaster);
    35. void handleSpinboxValueChange(int submaster);
    36.  
    37. private:
    38. QList<SubMaster *> m_submasters;
    39. };
    40.  
    41. #endif // TABWIDGET_H
    42.  
    43. #include <QVBoxLayout>
    44. #include <QSignalMapper>
    45.  
    46. #include "TabWidget.h"
    47. #include "ParentSignals.h"
    48.  
    49. TabWidget::TabWidget(QWidget *parent) : QWidget(parent)
    50. {
    51. QVBoxLayout * layout = new QVBoxLayout();
    52.  
    53. QSignalMapper * buttonOneMapper = new QSignalMapper(this);
    54. QSignalMapper * buttonTwoMapper = new QSignalMapper(this);
    55. QSignalMapper * buttonThreeMapper = new QSignalMapper(this);
    56. QSignalMapper * sliderMapper = new QSignalMapper(this);
    57. QSignalMapper * spinboxMapper = new QSignalMapper(this);
    58.  
    59. for(int i = 0; i < 18; ++i)
    60. {
    61. SubMaster * sub_master = new SubMaster();
    62.  
    63. connect(sub_master, SIGNAL(buttonOneClicked()), buttonOneMapper, SLOT(map()));
    64. buttonOneMapper->setMapping(sub_master, i);
    65. connect(buttonOneMapper, SIGNAL(mapped(int)), this, SIGNAL(buttonOneClicked(int)));
    66.  
    67. connect(sub_master, SIGNAL(buttonTwoClicked()), buttonTwoMapper, SLOT(map()));
    68. buttonTwoMapper->setMapping(sub_master, i);
    69. connect(buttonTwoMapper, SIGNAL(mapped(int)), this, SIGNAL(buttonTwoClicked(int)));
    70.  
    71. connect(sub_master, SIGNAL(buttonThreeClicked()), buttonThreeMapper, SLOT(map()));
    72. buttonThreeMapper->setMapping(sub_master, i);
    73. connect(buttonThreeMapper, SIGNAL(mapped(int)), this, SIGNAL(buttonThreeClicked(int)));
    74.  
    75. connect(sub_master, SIGNAL(sliderValueChanged()), sliderMapper, SLOT(map()));
    76. sliderMapper->setMapping(sub_master, i);
    77. connect(sliderMapper, SIGNAL(mapped(int)), this, SLOT(handleSliderValueChange(int)));
    78.  
    79. connect(sub_master, SIGNAL(spinboxValueChanged()), spinboxMapper, SLOT(map()));
    80. spinboxMapper->setMapping(sub_master, i);
    81. connect(spinboxMapper, SIGNAL(mapped(int)), this, SLOT(handleSpinboxValueChange(int)));
    82.  
    83. layout->addWidget(sub_master);
    84.  
    85. m_submasters.push_back(sub_master);
    86. }
    87.  
    88. this->setLayout(layout);
    89. }
    90.  
    91. TabWidget::~TabWidget()
    92. {
    93.  
    94. }
    95.  
    96. void TabWidget::handleSliderValueChange(int submaster)
    97. {
    98. int slider_value = m_submasters.at(submaster)->currentSliderValue();
    99. emit sliderValueChanged(submaster, slider_value);
    100. }
    101.  
    102. void TabWidget::handleSpinboxValueChange(int submaster)
    103. {
    104. int spinbox_value = m_submasters.at(submaster)->currentSpinBoxValue();
    105. emit spinboxValueChanged(submaster, spinbox_value);
    106. }
    107.  
    108.  
    109. #ifndef SUBMASTER_H
    110. #define SUBMASTER_H
    111.  
    112. #include <QtGui>
    113. #include <QtCore>
    114.  
    115. class SubMaster : public QWidget
    116. {
    117. Q_OBJECT
    118.  
    119. public:
    120. SubMaster(QWidget * parent = 0);
    121. virtual ~SubMaster();
    122.  
    123. int currentSliderValue() { return slider->value(); }
    124. int currentSpinBoxValue() { return spin_box->value(); }
    125.  
    126. signals:
    127. void buttonOneClicked();
    128. void buttonTwoClicked();
    129. void buttonThreeClicked();
    130.  
    131. void sliderValueChanged();
    132. void spinboxValueChanged();
    133.  
    134. private:
    135. QPushButton * button_one;
    136. QPushButton * button_two;
    137. QPushButton * button_three;
    138.  
    139. QSlider * slider;
    140. QSpinBox * spin_box;
    141. };
    142.  
    143. #endif // SUBMASTER_H
    144.  
    145. #include "SubMaster.h"
    146.  
    147. SubMaster::SubMaster(QWidget * parent) : QWidget(parent)
    148. {
    149. button_one = new QPushButton("One");
    150. connect(button_one, SIGNAL(clicked()), this, SIGNAL(buttonOneClicked()));
    151.  
    152. button_two = new QPushButton("Two");
    153. connect(button_two, SIGNAL(clicked()), this, SIGNAL(buttonTwoClicked()));
    154.  
    155. button_three = new QPushButton("Three");
    156. connect(button_three, SIGNAL(clicked()), this, SIGNAL(buttonThreeClicked()));
    157.  
    158. slider = new QSlider();
    159. connect(slider, SIGNAL(valueChanged(int)), this, SIGNAL(sliderValueChanged()));
    160.  
    161. spin_box = new QSpinBox();
    162. connect(spin_box, SIGNAL(valueChanged(int)), this, SIGNAL(spinboxValueChanged()));
    163.  
    164. connect(slider, SIGNAL(valueChanged(int)), spin_box, SLOT(setValue(int)));
    165. connect(spin_box, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
    166.  
    167. QHBoxLayout * layout = new QHBoxLayout();
    168. layout->addWidget(button_one);
    169. layout->addWidget(button_two);
    170. layout->addWidget(button_three);
    171. layout->addWidget(slider);
    172. layout->addWidget(spin_box);
    173.  
    174. this->setLayout(layout);
    175. }
    176.  
    177. SubMaster::~SubMaster()
    178. {
    179. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JimDaniel; 21st June 2008 at 17:39.

  2. #2
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom Widget - First Steps

    JimDaniel,

    Thank you again for all the help. I'm attaching a screenshot, so you can see that I'm not just copying your work.

    Everything has gone well so far, I've gotten the custom widget to run withing my existing program, and I've been able to make it look the same as the older individual widgets did. The problem I'm having now is regarding signals and slots.

    I have the master layout for the custom widget in TabWidget.h, as per your suggestions. However, in my "MainWindowImpl.cpp" file, where the rest of my slots are

    Qt Code:
    1. connect(TabWidget,SIGNAL(buttonOneClicked(int submaster,int)),this,SLOT(bumpUp1(int submaster,int)));
    To copy to clipboard, switch view to plain text mode 

    Returns an:

    error: expected primary-expression before ‘,’ token

    I've tried a bunch of different things-- I tried making an instance of the class, which didnt work, and I tried connecting directly to the individual submasters themselves, which didnt work.

    For example:

    Qt Code:
    1. TabWidget * tab_widget = new TabWidget();
    2. connect(tab_widget,SIGNAL(buttonOneClicked(int submaster,int)),this,SLOT(bumpUp1(int submaster,int)));
    To copy to clipboard, switch view to plain text mode 

    Builds, but on clicking the button, returns

    Object::connect: No such signal TabWidget::buttonOneClicked(int submaster,int)
    Object::connect: (receiver name: 'MainWindow')

    It occurred to me that all other widgets are referenced by name, and not by class.. is that the problem that I'm having? How would I go about attaching a name to this custom widget? Or is that not the problem at all?

    Thank you again for your assistance
    Attached Images Attached Images

  3. #3
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Custom Widget - First Steps

    Cool, app looks slick. How are you styling the GUI if you don't mind me asking?

    Your problem in this case is just that you can't have a descriptor for parameters in the connect:

    Qt Code:
    1. connect(tab_widget, SIGNAL(buttonOneClicked(int, int)), this, SLOT(bumpUp1(int, int)));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom Widget - First Steps

    Great, got it working beautifully now. I just styled it in QDesigner, and set every object to Inherit, aside from a few tweaks. I usually try to conform to UI guidelines, but since it's designed to be run in a dark room, a bright style wouldnt have really worked for me.

    Thanks for all of your help!

Similar Threads

  1. resizing events of a custom widget in a layout
    By Rooster in forum Qt Programming
    Replies: 7
    Last Post: 16th February 2008, 10:52
  2. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 14:06
  3. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 15:38
  4. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  5. Custom tab widget question
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 11:17

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