Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Icons for pushbutton

    General and Desktop
    4
    5
    19818
    Loading More Posts
    • 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
      f.vanalmetelevic.com last edited by

      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 Reply Quote 0
      • C
        Chuck Gao last edited by

        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 Reply Quote 0
        • L
          ludde last edited by

          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 Reply Quote 0
          • E
            Eddy last edited by

            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 Reply Quote 0
            • E
              Eddy last edited by

              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 Reply Quote 0
              • First post
                Last post