Results 1 to 8 of 8

Thread: Create a new button

  1. #1
    Join Date
    May 2006
    Posts
    57
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Create a new button

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Create a new button

    Quote Originally Posted by avis_phoenix
    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().

  3. #3
    Join Date
    May 2006
    Posts
    57
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: Create a new button

    I tried to make this class

    Qt Code:
    1. class ColorButton : public QAbstractButton
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ColorButton() {return;}
    6. explicit ColorButton(QWidget *parent,int x, int y, int alto, int ancho); //Define the button with parent, position, width and height
    7. QColor Color() {return Colore;} //return the actual color
    8. void setColor(QColor color); //Define the color of the button
    9. ~ColorButton();
    10. private:
    11. QPixmap *Col;
    12. QColor Colore;
    13. QLabel *Show;
    14. };
    To copy to clipboard, switch view to plain text mode 

    After i tried use this class with:

    Qt Code:
    1. 1 QVector<ColorButton> Button(32);
    2. 2 for (i =0; i< 4;i++)
    3. 3 {
    4. 4 for (int j = 0; j < 8;j++)
    5. 5 Button[i] = ColorButton(this,x+(i*21),y+(j*21),21,21); //intent for define each button
    6. }
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create a new button

    Quote Originally Posted by avis_phoenix
    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).

  5. #5
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create a new button

    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:

    Qt Code:
    1. QVector<ColorButton*> Button(32);
    2. for (i =0; i< 4;i++)
    3. {
    4. for (int j = 0; j < 8;j++)
    5. Button[i] = new ColorButton(this,x+(i*21),y+(j*21),21,21);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Save yourself some pain. Learn C++ before learning Qt.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create a new button

    Quote Originally Posted by Chicken Blood Machine
    your buttons will instantly go out of scope.
    Also you can't copy widgets.

  7. #7
    Join Date
    May 2006
    Posts
    57
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: Create a new button

    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:perator=(const ColorButton&)':
    D:/Qt/4.1.2/include/QtGui/../../src/gui/widgets/qabstractbutton.h:139:
    error: `QAbstractButton& QAbstractButton:perator=(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

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Create a new button

    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*>.
    J-P Nurmi

Similar Threads

  1. How to create an Insert/Submit button for a form.
    By fnmblot in forum Qt Programming
    Replies: 5
    Last Post: 4th August 2006, 16:18
  2. Round Button
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2006, 11:32
  3. Push Button problem!!
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2006, 16:31
  4. Push button double click
    By curtisw in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2006, 16:40
  5. QLabel on a Button
    By cwalsh in forum Newbie
    Replies: 4
    Last Post: 12th January 2006, 16:06

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.