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: Set Tab Icon with code
QtWS25 Last Chance

SOLVED: Set Tab Icon with code

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 9.1k 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.
  • Q Offline
    Q Offline
    Qatto
    wrote on last edited by
    #1

    Hello Developers,

    I have been trying to set an icon for a tab in a tabWidget using code but failed so far. Its easier with the form designer but I need to use code to make my app dynamic.
    Here's the code for mainwindow.cpp, the rest of the files are the default for a MainWindow project.
    Please show me how to set icons with size 20x20p for both tab1 and tab2 below.

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QTabWidget *tabWidget = new QTabWidget;
    QWidget *tab1 = new QWidget;
    QWidget *tab2 = new QWidget;
    
    setCentralWidget(tabWidget);
    
    tabWidget->insertTab(0, tab1, "Tab 1");
    tabWidget->insertTab(1, tab2, "Tab 2");
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    Thanks in advance

    Web/Desktop Developer

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SergioDanielG
      wrote on last edited by
      #2

      Hi Qkato.
      Did you try insert tabs with this option?

      @int QTabWidget::insertTab(int index, QWidget * page, const QIcon & icon, const QString & label)@

      Hope it's util.
      Regards.

      www.ftatv.com.ar El foro argentino de la TV libre

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        Read the manual please:

        @
        int QTabWidget::insertTab(int index, QWidget * page, const QIcon & icon, const QString & label)
        @

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          or even QTabWidget::setTabIcon() ...

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qatto
            wrote on last edited by
            #5

            Yes, I can see the parameters but cant seem to get it right.
            Please illustrate how I can add an icon that's a resource with path "image/icon.png"

            Web/Desktop Developer

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @QIcon(“:/image/icon.png”)@

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qatto
                wrote on last edited by
                #7

                Tried the example, still can't get the icon on the tab. Is it about having the size of the image defined too, and if so how is it done?

                Web/Desktop Developer

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

                  Please show you full code and resource file

                  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
                  • Q Offline
                    Q Offline
                    Qatto
                    wrote on last edited by
                    #9

                    Thanks for all your posts, When mcosta talked of resource file I took a look at it and there was a prefix for images. Its why the icon wasn't visible all along. Now all is fine.

                    Thanks so much for the help.

                    Web/Desktop Developer

                    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