PDA

View Full Version : Create a new button



avis_phoenix
1st September 2006, 23:06
How I make my own button?
my class of a button, whit diferent style, shape, and actions?

(I tried to make an special color button)

Thanks

wysota
1st September 2006, 23:12
How I make my own button?

shape, and actions?

Subclass QPushButton or QAbstractButton.

whit diferent style
Subclass QStyle or one of its subclasses.


(I tried to make an special color button)
If you just want to change colours, maybe it will be enough if you modify its QWidget::palette() using QWidget::setPalette().

avis_phoenix
2nd September 2006, 05:22
I tried to make this class



class ColorButton : public QAbstractButton
{
Q_OBJECT
public:
explicit ColorButton() {return;}
explicit ColorButton(QWidget *parent,int x, int y, int alto, int ancho); //Define the button with parent, position, width and height
QColor Color() {return Colore;} //return the actual color
void setColor(QColor color); //Define the color of the button
~ColorButton();
private:
QPixmap *Col;
QColor Colore;
QLabel *Show;
};


After i tried use this class with:


1 QVector<ColorButton> Button(32);
2 for (i =0; i< 4;i++)
3 {
4 for (int j = 0; j < 8;j++)
5 Button[i] = ColorButton(this,x+(i*21),y+(j*21),21,21); //intent for define each button
}


But gcc say:

cannot allocate and object of type 'ColorButton' in the line 5
because the following virtual functions are abstracts

then how make the class type abstractsbuttons to create a new button?

Sorry for my bad english

jacek
2nd September 2006, 12:01
cannot allocate and object of type 'ColorButton' in the line 5
because the following virtual functions are abstracts

then how make the class type abstractsbuttons to create a new button?
You must implement all abstract methods (the compiler should tell you which ones are missing).

Chicken Blood Machine
2nd September 2006, 20:04
Also, you must create your buttons on the heap not the stack. As is, your code will not work as your buttons will instantly go out of scope. Try:



QVector<ColorButton*> Button(32);
for (i =0; i< 4;i++)
{
for (int j = 0; j < 8;j++)
Button[i] = new ColorButton(this,x+(i*21),y+(j*21),21,21);
}

jacek
2nd September 2006, 20:29
your buttons will instantly go out of scope.
Also you can't copy widgets.

avis_phoenix
3rd September 2006, 19:20
Well i implement the abstracts metods and now gcc don't say
"cannot allocate and object of type 'ColorButton' in the line 5
because the following virtual functions are abstracts"

but say :

In copy constructor `ColorButton::ColorButton(const ColorButton&)':
D:/Qt/4.1.2/include/QtGui/../../src/gui/widgets/qabstractbutton.h:139:
error: `QAbstractButton::QAbstractButton(const QAbstractButton&)' is private
error: within this context
In member function `ColorButton& ColorButton::operator=(const ColorButton&)':
D:/Qt/4.1.2/include/QtGui/../../src/gui/widgets/qabstractbutton.h:139:
error: `QAbstractButton& QAbstractButton::operator=(const QAbstractButton&)' is private
error: within this context

in the line

Button[i] = ColorButton(this,x+(i*21),y+(j*21),21,21);

don´t work if i put Button[i] = new ColorButton(this,x+(i*21),y+(j*21),21,21);

Help me please

jpn
3rd September 2006, 19:56
Chicken Blood Machine and jacek already gave you the solution.

You cannot copy widgets, so you cannot store them into QVector as objects. Store pointers instead.
You have to change the type of the vector from QVector<ColorButton> to QVector<ColorButton*>.