c++qtqchart

How to scale QPixmap into the same size


I declared two icons in my GUI and resized them by using Adobe illustrator. But they still showed in different size in my GUI. Is there any method to resize them in Qt? Thanks a lot!

void WToolbar::setCandlesColor()
{
    QIcon icon("./src/icon/toolbar/chart-histogram-01.png");
    QPixmap pixmap = icon.pixmap(QSize(30, 30)); // set desired size here

    SetColor = std::make_shared<QAction>(QIcon(pixmap), QString("Button"), toolbar_.get());
    SetColor->setToolTip("Green Bullish/ Red Bearish");
    SetColor->setIconVisibleInMenu(true);
    toolbar_->addAction(SetColor.get());

    connect(SetColor.get(), &QAction::triggered, plotter_.get(), &WPlotter::colorSwitch);
    connect(SetColor.get(), &QAction::triggered, this, &WToolbar::iconSwitch);
}

void WToolbar::setMA5()
{
    QIcon icon("./src/icon/toolbar/chart-line-up-ma5.png");
    QPixmap pixmap = icon.pixmap(QSize(30, 30)).scaled(QSize(30, 30), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);; // set desired size here

    PlotMA5 = std::make_shared<QAction>(QIcon(pixmap), QString("Button"), toolbar_.get());
    PlotMA5->setToolTip("show/hide MA5");
    PlotMA5->setIconVisibleInMenu(true);
    toolbar_->addAction(PlotMA5.get());

    connect(PlotMA5.get(), &QAction::triggered, plotter_.get(), &WPlotter::plotMA5);
}

after I clicked the first icon, it got bigger... enter image description here


Solution

  • I noticed that this line is unavailable:

    QPixmap pixmap = icon.pixmap(QSize(30, 30));
    

    But I can use stylesheet as a workaround. This is my settings:

    void WToolbar::SetToolbarStyle()
    {
        QString style = " \
                        QToolBar { \
                            background-color: rgb(28, 27, 29); \
                            spacing: 5px; \
                        } \
                        QToolButton { \
                            padding: 5px; \
                            margin: 0px; \
                            border: none; \
                            border-radius: 3px; \
                            background-color: none; \
                        } \
                        QToolButton:hover { \
                            background-color: rgb(28, 27, 29); \
                        }";
    
        toolbar_->setStyleSheet(style);
    }
    

    And the size of all icons are aligned.