Results 1 to 12 of 12

Thread: Image on button

  1. #1
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Image on button

    Hello all,

    some help please with this puzzle:

    I'm trying to put an image on a pushbutton. I wrote this program in Qt3 on Linux, later I rewrote it to Qt4. So no Qt3 support or anything, only Qt4 left.
    Now I'm trying to compile it on Windows. But I cannot get the image to show on the pushbutton on Windows. Works on Linux.

    This is what I'm trying to do: Can somebody see the problem with this?

    Qt Code:
    1. from knop.h:
    2. QPixmap pWit;
    3. QPalette palWit;
    4. QString progPath;
    5.  
    6. from knop.c:
    7.  
    8. knop::knop(QWidget* parent)
    9. : QWidget(parent)
    10. {
    11. setMaximumSize( QSize( 50, 50 ) );
    12.  
    13. but = new QPushButton("but", this);
    14. but->setGeometry( QRect( 0, 0, 50, 50 ) );
    15. but->setCheckable(true);
    16. but->setFont(QFont("Bitstream Vera Sans",22,QFont::Bold));
    17. but->setFlat(true);
    18.  
    19. e = new QLineEdit("e", this);
    20. e->setGeometry( QRect( 5, 5, 40, 10 ) );
    21. QFont e_font( e->font() );
    22. e_font.setPointSize( 10 );
    23. e->setFont( e_font );
    24. e->setFrame( FALSE );
    25.  
    26. resize( QSize(50, 50).expandedTo(minimumSizeHint()) );
    27. connect( but, SIGNAL(clicked()), SLOT(butClick()) );
    28. init();
    29.  
    30. }
    31.  
    32. void knop::init()
    33. {
    34. #ifdef _WIN32
    35. progPath = "/home/bartv/progs/sudoku/"; //linux
    36. #else
    37. progPath = "D:/qt/progs/sudoku/"; //windows
    38. #endif
    39.  
    40. pWit.load("knopWit.png");
    41.  
    42. palWit.setBrush(QPalette::Active,QPalette::Window,QBrush(pWit));
    43. palWit.setBrush(QPalette::Active,QPalette::Base,QColor(255,255,255));
    44. palWit.setBrush(QPalette::Active,QPalette::Text,QColor(0,0,0));
    45. palWit.setBrush(QPalette::Active,QPalette::ButtonText,QColor(0,0,0));
    46. palWit.setBrush(QPalette::Inactive,QPalette::Window,QBrush(pWit));
    47. palWit.setBrush(QPalette::Inactive,QPalette::Base,QColor(255,255,255));
    48. palWit.setBrush(QPalette::Inactive,QPalette::Text,QColor(0,0,0));
    49. palWit.setBrush(QPalette::Inactive,QPalette::ButtonText,QColor(0,0,0));
    50.  
    51. but->setPallete(palWit);
    52. e->setPalette(palWit);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Image on button

    Where do you use progPath?

    PS. You might find QCoreApplication::applicationDirPath() useful.

  3. #3
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Image on button

    Thanks Jacek,

    but not the answer I was looking for at te moment.

    The solution to the problem at hand: put an label behind the button.
    In Qt4 it's not posible anymore to put an image on an button. Only labels.
    So label behind button, alpha of the button to 0.

    Qt Code:
    1. label = new QLabel("label", this);
    2. label->setGeometry( QRect( 0, 0, 50, 50 ) );
    3. label->setText("");
    4.  
    5. but = new QPushButton("but", this);
    6. but->setGeometry( QRect( 0, 0, 50, 50 ) );
    7. but->setCheckable(true);
    8. but->setFont(QFont("Bitstream Vera Sans",22,QFont::Bold));
    9. but->setFlat(true);
    10.  
    11. e = new QLineEdit("e", this);
    12. e->setGeometry( QRect( 5, 5, 40, 10 ) );
    13. QFont e_font( e->font() );
    14. e_font.setPointSize( 10 );
    15. e->setFont( e_font );
    16. e->setFrame( FALSE );
    17.  
    18. resize( QSize(50, 50).expandedTo(minimumSizeHint()) );
    19. connect( but, SIGNAL(clicked()), SLOT(butClick()) );
    20. init();
    21.  
    22. }
    23.  
    24. knop::~knop(){}
    25.  
    26. void knop::init()
    27. {
    28. pWit.load("/home/bartv/progs/sudoku/include/knopWit.png");
    29. pRood.load("/home/bartv/progs/sudoku/include/knopRood.png");
    30. pGroen.load("/home/bartv/progs/sudoku/include/knopGroen.png");
    31.  
    32.  
    33. palWit.setColor(QPalette::Base,QColor(255,255,255));
    34. palWit.setColor(QPalette::Button,QColor(255,255,255,0));
    35.  
    36. palRood.setColor(QPalette::Base,QColor(255,0,0));
    37.  
    38. palGroen.setColor(QPalette::Base,QColor(0,255,0));
    39.  
    40. palClick.setColor(QPalette::Base,QColor(150,150,150));
    41. palClick.setColor(QPalette::Button,QColor(150,150,150,255));
    42.  
    43. label->setPixmap(pWit);
    44. but->setPalette(palWit);
    45. e->setPalette(palWit);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    Bart.

  4. #4
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Image on button

    ps.

    -cut- from "QCoreApplication::applicationDirPath()"
    Warning: On Unix, this function assumes that argv[0] contains the file name of the executable (which it normally does). It also assumes that the current directory hasn't been changed by the application.

    Sounds like it's the same as "QDir::Path()".

    Cheers,
    Bart.

  5. #5
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Image on button

    Quote Originally Posted by windkracht8
    In Qt4 it's not posible anymore to put an image on an button.
    Yet you say you can do it in Linux!!

    Quote Originally Posted by windkracht8
    pWit.load("/home/bartv/progs/sudoku/include/knopWit.png");
    pRood.load("/home/bartv/progs/sudoku/include/knopRood.png");
    pGroen.load("/home/bartv/progs/sudoku/include/knopGroen.png");
    Do these paths exist under Windows?

  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: Image on button

    Quote Originally Posted by windkracht8
    Sounds like it's the same as "QDir::Path()".
    Yes, but it's a static method.

  7. #7
    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: Image on button

    Quote Originally Posted by windkracht8
    In Qt4 it's not posible anymore to put an image on an button.
    It is possible as it works for me (on both Linux and windows) --- you just have to use correct paths.

  8. #8
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Image on button

    Quote Originally Posted by jacek
    It is possible as it works for me (on both Linux and windows) --- you just have to use correct paths.
    Could you please post your code on howto do this here please.

    To clear up things:
    This is in init: (Could have been done with QDir:rogPath(), but not implemented yet)
    Qt Code:
    1. #ifdef _WIN32
    2. progPath = "D:/qt/progs/sudoku/"; //windows
    3. #else
    4. progPath = "/home/bartv/progs/sudoku/"; //linux
    5. #endif
    6.  
    7. pWit.load("knopWit.png");
    8.  
    9. palWit.setColor(QPalette::Base,QColor(255,255,255));
    10. palWit.setColor(QPalette::Button,QColor(255,255,255,0));
    11. palWit.setColor(QPalette::Text,QColor(0,0,0,255));
    12. palWit.setColor(QPalette::ButtonText,QColor(0,0,0,255));
    13.  
    14. label->setPixmap(pWit);
    15. but->setPalette(palWit);
    16. e->setPalette(palWit);
    To copy to clipboard, switch view to plain text mode 

    @Brandybuck:
    With Qt3.3, not with Qt4 anymore, sorry for the mixup.

    Cheers,
    Bart.

  9. #9
    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: Image on button

    Quote Originally Posted by windkracht8
    Could you please post your code on howto do this here please.
    Try something like:
    Qt Code:
    1. pWit.load( QCoreApplication::applicationDirPath() + QDir::separator() + "knopWit.png" );
    2. but->setIcon( pWit );
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Image on button

    That give's me a small icon on the button. I need the entire image to be the entire button.
    So people see my image not the button.

    Can't put an image on the net at the moment, no ftp allowd here.

    But I got it to work. It's only a shame I need to put an label behind the button.

    Cheers,
    Bart.

  11. #11
    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: Image on button

    Quote Originally Posted by windkracht8
    That give's me a small icon on the button. I need the entire image to be the entire button. So people see my image not the button.
    Maybe QPushButton::setFlat() will be enough?

    Quote Originally Posted by windkracht8
    Can't put an image on the net at the moment, no ftp allowd here.
    Just post it as attachment (click "Manage Attachments" below the editor).

  12. #12
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Image on button

    All ready got that set.

    knop.cpp is an custom widget which is placed a couple of times in an frame on the main form.
    Complete code included. Maybe someone is interrested in a qt cross-platform sudoku game. It generates games on different levels.

    Now I just need to get it up and running on my phone. (windows mobile)

    knop.cpp:
    Qt Code:
    1. #include "knop.h"
    2. #include "bc.h"
    3.  
    4. #include <QVariant>
    5. #include <QLineEdit>
    6. #include <QPushButton>
    7. #include <QString>
    8. #include <QApplication>
    9. #include <QMessageBox>
    10. #include <QTimer>
    11.  
    12. knop::knop(QWidget* parent)
    13. : QWidget(parent)
    14. {
    15. setMaximumSize( QSize( 50, 50 ) );
    16.  
    17. label = new QLabel("label", this);
    18. label->setGeometry( QRect( 0, 0, 50, 50 ) );
    19. label->setText("");
    20.  
    21. but = new QPushButton("but", this);
    22. but->setGeometry( QRect( 0, 0, 50, 50 ) );
    23. but->setCheckable(true);
    24. but->setFont(QFont("Bitstream Vera Sans",22,QFont::Bold));
    25. but->setFlat(true);
    26.  
    27. e = new QLineEdit("e", this);
    28. e->setGeometry( QRect( 5, 5, 40, 10 ) );
    29. QFont e_font( e->font() );
    30. e_font.setPointSize( 10 );
    31. e->setFont( e_font );
    32. e->setFrame( FALSE );
    33.  
    34. resize( QSize(50, 50).expandedTo(minimumSizeHint()) );
    35. connect( but, SIGNAL(clicked()), SLOT(butClick()) );
    36. init();
    37.  
    38. }
    39.  
    40. knop::~knop(){}
    41.  
    42. void knop::init()
    43. {
    44. progPath = QCoreApplication::applicationDirPath() + "/";
    45.  
    46. pWit.load(progPath + "include/knopWit.png");
    47. pRood.load(progPath + "include/knopRood.png");
    48. pGroen.load(progPath + "include/knopGroen.png");
    49.  
    50. palWit.setColor(QPalette::Base,QColor(255,255,255));
    51. palWit.setColor(QPalette::Button,QColor(255,255,255,0));
    52. palWit.setColor(QPalette::Text,QColor(0,0,0,255));
    53. palWit.setColor(QPalette::ButtonText,QColor(0,0,0,255));
    54.  
    55. palRood.setColor(QPalette::Base,QColor(255,0,0));
    56. palRood.setColor(QPalette::Text,QColor(0,0,0,255));
    57. palRood.setColor(QPalette::ButtonText,QColor(0,0,0,255));
    58.  
    59. palGroen.setColor(QPalette::Base,QColor(0,255,0));
    60. palGroen.setColor(QPalette::Text,QColor(0,0,0,255));
    61. palGroen.setColor(QPalette::ButtonText,QColor(0,0,0,255));
    62.  
    63. palClick.setColor(QPalette::Base,QColor(200,200,200));
    64. palClick.setColor(QPalette::Button,QColor(200,200,200,255));
    65. palClick.setColor(QPalette::Text,QColor(0,0,0,255));
    66. palClick.setColor(QPalette::ButtonText,QColor(0,0,0,255));
    67.  
    68. label->setPixmap(pWit);
    69. but->setPalette(palWit);
    70. e->setPalette(palWit);
    71.  
    72. }
    73. void knop::setNum(QString num)
    74. {
    75. but->setText(num);
    76. current = num;
    77. e->setText("");
    78. }
    79.  
    80. void knop::butClick()
    81. {
    82. bC* bC1 = new bC(knopRij,knopCol);
    83. QApplication::postEvent(this->parent()->parent(), bC1);
    84. }
    85. void knop::unClick()
    86. {
    87. but->setChecked(false);
    88. but->setPalette(palWit);
    89. e->setPalette(palWit);
    90. }
    91. void knop::hint(QString hoort)
    92. {
    93. QTimer::singleShot(2000, this, SLOT(terug()));
    94. but->setText(hoort);
    95. but->setPalette(palGroen);
    96. e->setPalette(palGroen);
    97. }
    98. void knop::terug()
    99. {
    100. but->setText(current);
    101. but->setPalette(palWit);
    102. e->setPalette(palWit);
    103. }
    104. void knop::setRood()
    105. {
    106. but->setPalette(palRood);
    107. e->setPalette(palRood);
    108. QTimer::singleShot(5000, this, SLOT(terug()));
    109. }
    110. void knop::setGroen()
    111. {
    112. but->setPalette(palGroen);
    113. e->setPalette(palGroen);
    114. QTimer::singleShot(5000, this, SLOT(terug()));
    115. }
    116. void knop::clearEdit()
    117. {
    118. e->setText("");
    119. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files

Similar Threads

  1. "sensitising" an image
    By TheKedge in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2006, 07:21
  2. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 10:45
  3. problem with the back ground image
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2006, 21:34
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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.