Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Icons for pushbutton
QtWS25 Last Chance

Icons for pushbutton

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 20.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    f.vanalmetelevic.com
    wrote on last edited by
    #1

    I would like to have my own icons on some pushbuttons. I have two icons, one for the released state and one for the pressed state.
    I tried it like this but this didn't work :

    @ QIcon icon;
    QImage im1(":/Images/checked.png");
    icon.addPixmap(QPixmap::fromImage(im1),QIcon::Normal, QIcon::On);
    icon.addPixmap(QPixmap::fromImage(im1),QIcon::Active, QIcon::On);
    icon.addPixmap(QPixmap::fromImage(im1),QIcon::Selected, QIcon::On);
    QImage im2(":/Images/unchecked.png");
    icon.addPixmap(QPixmap::fromImage(im2),QIcon::Normal, QIcon::Off);
    icon.addPixmap(QPixmap::fromImage(im2),QIcon::Active, QIcon::Off);
    icon.addPixmap(QPixmap::fromImage(im2),QIcon::Selected, QIcon::Off);

    QPushButton *pb = new QPushButton (this);
    pb->setIcon(icon);
    pb->setIconSize(pb->size());
    pb->setFocusPolicy(Qt::NoFocus);

    @

    I always have only one icon.
    What do I miss ? If I add the icons to the different states of the icon, I should get the right icons automatically, no ?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chuck Gao
      wrote on last edited by
      #2

      I'm sorry, your code is wrong. Basiclly, you can done this by using style-sheet. Or, listening the press/release events, and handle the icon setting yourself.

      Something like this by using style-sheet:
      @
      QPushButton:pressed {
      background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
      stop: 0 #dadbde, stop: 1 #f6f7fa);
      }
      @

      Chuck

      1 Reply Last reply
      0
      • L Offline
        L Offline
        ludde
        wrote on last edited by
        #3

        I'm sorry, but I cannot see why that code is necessarily wrong...
        However, I think a push button by default does not have states.
        You need to also do
        @pb->setCheckable(true);@
        Cannot promise that that will make it work, but it might. :)

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          You can do it in Qt Creator when editing your form.
          Use a resource file with the needed images.

          select your pushbutton and go to the properties.
          select an icon.
          You will see severall states. make sure you pick the right one forActive On and Active off.

          That's it.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • EddyE Offline
            EddyE Offline
            Eddy
            wrote on last edited by
            #5

            this is the code taken from my ui_dialog.h file

            This should help you out with your code. Look at the red.png line : all the others remain the same.

               @ QIcon icon;
                icon.addFile(QString::fromUtf8(":/blue.png"), QSize(), QIcon::Normal, QIcon::On);
                icon.addFile(QString::fromUtf8(":/blue.png"), QSize(), QIcon::Disabled, QIcon::Off);
                icon.addFile(QString::fromUtf8(":/blue.png"), QSize(), QIcon::Active, QIcon::Off);
                icon.addFile(QString::fromUtf8(":/red.png"), QSize(), QIcon::Active, QIcon::On);
                icon.addFile(QString::fromUtf8(":/blue.png"), QSize(), QIcon::Selected, QIcon::Off);
                pushButton->setIcon(icon);
                pushButton->setCheckable(true);@
            

            i really like to do things like this using Qt Designer and see all the options immediately in the dialog.

            Qt Certified Specialist
            www.edalsolutions.be

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved