PDA

View Full Version : custom button-panel widget; subclass advice?



vonCZ
11th November 2007, 12:28
I need to design a custom panel that will pop-up next to some of my QPushButtons when they are mouseOver'ed. Initially, the panels will contain only 2 types of widgets: varying numbers of QPushButtons and a QSlider, although I'd like to keep open the option to include additional widgets later.

So I figure I'll create a new BuPanel subclass... but looking over chapter 5 in "C++ GUI Programming with Qt4" I'm not sure what approach I should take. Their HexSpinBox example is a simple sub-class from QSpinBox, but in my case I'll have at least 2 primary widgets... would you recommend creating a new class derived from both QPushButton and QSlider? (or just QPushButton, and include a "class QSlider;" declaration in the header?) And will I have to include a parameter in the constructor which sets the number of buttons in a given panel?

edit: here's an example-

http://www.beg.utexas.edu/coastal/wrl/del/panel.png

mousing over "shorelines" brings up the panel with buttons & slider (red triangle) to the right. User can either click buttons or drag the slider to change selection.

wysota
11th November 2007, 21:23
I'd say that this is a regular menu and you can implement it as one... but if you insist on having a "panel", I'd suggest creating a widget containing the "panel" and use event filters installed on the widgets that are to be handled by the panel to show/hide the panel based on receving an enter/leave event. No need to subclass anything (besides QWidget, of course)...

vonCZ
26th November 2007, 17:13
thanks Wysota- i've subclassed QWidget and have a new widget, BuPan. I've a pretty simple question about declaring pointers in my header file; the constructor contains the following:



BuPanel::BuPanel(QWidget *parent, int numBu, int slid) : QWidget(parent)
{

for(int a=0; a<numBu; a++)
{
pushB[a] = new QPushButton;
//etc...
}
//etc...


and i have



QPushButton *pushB[20]; //private


in the header. "20" is the maximum number of buttons I'll have in any panel, but most will have far fewer. Is this bad coding practice? ...to declare unnecessary pointers that won't be used/instantiated?

wysota
26th November 2007, 18:01
Very bad :) Either don't store pointers at all or use QList<QPushButton*> instead.