如果鼠標位於欄的填充部分上,如何更改QProgressBar
的顏色和/或文本(格式)?將鼠標移動到QProgressBar上的顏色/文字
我已經堆疊了多個QProgressBar
s在彼此之上,每個顯示比以前更多的內容。
我想突出鼠標懸停在鼠標懸停下的最大的酒吧,並顯示一些酒吧特定的文本。但是,酒吧是相同的大小,因此我想識別酒吧的填充區域。
例如,如果鼠標在第三棒,我想強調的部分從左側第三條,並顯示具體到第三欄文本。
這是我使用的堆疊ProgressBars代碼:
class MultiProgressBar : public QWidget
{
Q_OBJECT
public:
MultiProgressBar(QWidget* parent = Q_NULLPTR) : QWidget(parent), layout(new QStackedLayout(this))
{
layout->setMargin(0);
layout->setStackingMode(QStackedLayout::StackAll);
}
void insertBar(QColor const& color)
{
auto bar = new QProgressBar();
bar->setTextVisible(false);
bar->setRange(0, 10000);
QPalette palette = this->palette();
palette.setColor(QPalette::Highlight, QColor(color.red(), color.green(), color.blue(), 100));
palette.setColor(QPalette::Base, QColor(color.red(), color.green(), color.blue(), 0));
bar->setPalette(palette);
layout->addWidget(bar);
progress_bars.push_back(bar);
}
public slots:
void setValues(const std::vector<int>& values, const std::vector<std::string>& names)
{
if (values.size() < progress_bars.size())
{
for (auto* widget : progress_bars)
{
layout->removeWidget(widget);
}
progress_bars.clear();
}
while (progress_bars.size() < values.size())
{
insertBar(QColor(0x46, 0xA1, 0xD9, 255));
}
for (auto i = 0; i < progress_bars.size(); ++i)
{
progress_bars[ i ]->setValue(values[ i ]);
// progress_bars[ i ]->setFormat(QString::fromStdString(names[ i ]));
}
}
private:
std::vector< QProgressBar* > progress_bars;
QStackedLayout* layout;
};
工程就像一個魅力。我特別喜歡詳細的故障和評論! – Trevir