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. [SOLVED] Button with Icons via code
QtWS25 Last Chance

[SOLVED] Button with Icons via code

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.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.
  • Andy314A Offline
    Andy314A Offline
    Andy314
    wrote on last edited by Andy314
    #1

    I try to create a bool-indcator button. So I have some question about images.
    I have a recoure file with windows ico files. Every iconfile has 3 incons integrated with different size. This is very practicable, because I can change in dialog designer the icon-size and it seem as it puts the best matching icon of the 3 for this size.

    But how can I do this via code?

            QPixmap pix(":/images/Games.ico");
            QIcon icon(pix);
            c->setIcon(icon);
            c->setIconSize(QSize(16,16));
    

    This picks always the 48x48 icon and scales it to 16x16, what looks bad.
    How can it pick a subicon with a special size from a resource file ???

    I want 2 different icon for the On and Off state for this button. Via Designer I can configure different icons for the 2 states (Normal on/Normal Off property).
    How can I make this via code? I found only the function setIcon() which set on/off state to the same icon.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi,
      look at this: https://forum.qt.io/topic/14047/icon-sizes-incorrect/3

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        Hi,

        instead of assigning the pixmap on QIcon constructor you shoud use addPixmap

        QIcon icon;
        icon.addPixmap(onPixmap, QIcon::Normal, QIcon::On);
        icon.addPixmap(offPixmap, QIcon::Normal, QIcon::Off);
        

        if you also need to set different pixmap depending on the size you can use QIcon::addFile()

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        ? 1 Reply Last reply
        0
        • M mcosta

          Hi,

          instead of assigning the pixmap on QIcon constructor you shoud use addPixmap

          QIcon icon;
          icon.addPixmap(onPixmap, QIcon::Normal, QIcon::On);
          icon.addPixmap(offPixmap, QIcon::Normal, QIcon::Off);
          

          if you also need to set different pixmap depending on the size you can use QIcon::addFile()

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @mcosta Yes, seems like the QIcon from pixmap constructor needs some improvement.

          M 1 Reply Last reply
          0
          • ? A Former User

            @mcosta Yes, seems like the QIcon from pixmap constructor needs some improvement.

            M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            @Wieland said:

            @mcosta Yes, seems like the QIcon from pixmap constructor needs some improvement.

            The standard constructor is enough for standard usage.
            Qt usually makes default things simple and provides API to use advanced feature (QMessageBox is another example)

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            0
            • Andy314A Offline
              Andy314A Offline
              Andy314
              wrote on last edited by
              #6

              I found out the "read all subicons" is integreated in the constructor yet.
              The problem was the indirect loading in a pixmap before.
              Now, it takes really the best matching iconsize, automatically.

                      QPushButton *c = new QPushButton(OnText, parent);
                      QIcon icon(":/images/Games.ico");
                      c->setIcon(icon);
                      c->setIconSize(QSize(16,16));
              
              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Cool! :-) Could you please post one of your icon files? I would like to check if this works with QtQuick, too.

                Andy314A 1 Reply Last reply
                0
                • ? A Former User

                  Cool! :-) Could you please post one of your icon files? I would like to check if this works with QtQuick, too.

                  Andy314A Offline
                  Andy314A Offline
                  Andy314
                  wrote on last edited by
                  #8

                  @Wieland
                  how can I upload files here ?

                  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