gpt4 book ai didi

c++ - Qt C++ - 在 QSplashScreen 中对齐 QProgressBar

转载 作者:太空狗 更新时间:2023-10-29 21:26:05 26 4
gpt4 key购买 nike

我通过子类化 QSplashScreen 将 QProgressBar 放入 QSplashScreen 中。它覆盖了 drawContents() 方法。

我以为我已经正确设置了几何图形,但它同时呈现在屏幕的顶部和底部。我不知道为什么。也许还有另一种对齐方式。数字是正确的,因为图像是 380x284,所以 19 高度的进度条应该向下 265 像素。

抱歉图片质量差,启动画面没有显示打印屏幕按钮。目前它只是一个 1 种颜色的白色启动画面,但正如您所见,顶部和底部的进度条(它们的颜色相同,是来自相机的照明)。

http://i.imgur.com/p1LoJ.jpg

另一个问题是 QSplashScreen 的 showMessage() 方法。我希望消息显示在进度条上方,右对齐...如果有人有任何想法如何做到这一点。

启动画面.cpp

#include "splashscreen.h"

SplashScreen::SplashScreen(QApplication *app, QWidget *parent) :
QSplashScreen(parent)
{
this->app = app;

this->setPixmap(QPixmap(":/images/splashscreen.png"));
this->setCursor(Qt::BusyCursor);

// if I dont make it a child, it *only* renders at the top
progress = new QProgressBar(this);
progress->setGeometry(0, 265, 380, 19); // puts it at bottom
progress->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
progress->setValue(0);
progress->setMaximum(100);
progress->setEnabled(true);

this->showMessage("Hello", Qt::AlignBottom);

connect(progress, SIGNAL(valueChanged(int)), this, SLOT(progressBarUpdated(int)));
}

void SplashScreen::drawContents(QPainter *painter)
{
QSplashScreen::drawContents(painter);
this->progress->render(painter);
}

void SplashScreen::progressBarUpdated(int value)
{
this->repaint();
this->app->processEvents();
}

启动画面.h

#ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H

#include <QSplashScreen>
#include <QProgressBar>
#include <QApplication>

class SplashScreen : public QSplashScreen
{
Q_OBJECT
public:
explicit SplashScreen(QApplication *app, QWidget *parent = 0);
QProgressBar *progress;
QWidget *spacer;
QApplication *app;

public slots:
void progressBarUpdated(int value);

protected:
void drawContents(QPainter *painter);

};

#endif // SPLASHSCREEN_H

主要.cpp

#include <QtGui/QApplication>
#include <time.h>

#include "splashscreen.h"
#include "mainwindow.h"

int main(int argc, char *argv[])
{
srand(time(0));
QApplication a(argc, argv);

SplashScreen *splash = new SplashScreen(&a);
splash->show();

// snip.. loading a ton of stuff into memory at startup
// if you're testing this you might have to sleep/timer here iono

MainWindow w;
splash->finish(&w);
w.show();

return app.exec();
}

最佳答案

您可以直接绘制进度,而无需创建 QProgressBar。例如:

sp.h:

#ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H

#include <QSplashScreen>
#include <QApplication>

class SplashScreen : public QSplashScreen
{
Q_OBJECT
public:
explicit SplashScreen(QApplication *app, QWidget *parent = 0);
int m_progress;
QApplication *app;

public slots:
void setProgress(int value)
{
m_progress = value;
if (m_progress > 100)
m_progress = 100;
if (m_progress < 0)
m_progress = 0;
update();
}

protected:
void drawContents(QPainter *painter);

};

#endif // SPLASHSCREEN_H

sp.cpp

#include "sp.h"

SplashScreen::SplashScreen(QApplication *aApp, QWidget *parent) :
QSplashScreen(parent), app(aApp), m_progress(0)

{
this->setPixmap(QPixmap(":/images/splashscreen.png"));
this->setCursor(Qt::BusyCursor);

this->showMessage("Hello", Qt::AlignBottom);
}

void SplashScreen::drawContents(QPainter *painter)
{
QSplashScreen::drawContents(painter);

// Set style for progressbar...
QStyleOptionProgressBarV2 pbstyle;
pbstyle.initFrom(this);
pbstyle.state = QStyle::State_Enabled;
pbstyle.textVisible = false;
pbstyle.minimum = 0;
pbstyle.maximum = 100;
pbstyle.progress = m_progress;
pbstyle.invertedAppearance = false;
pbstyle.rect = QRect(0, 265, 380, 19); // Where is it.

// Draw it...
style()->drawControl(QStyle::CE_ProgressBar, &pbstyle, painter, this);
}

也许这对你有帮助。

关于c++ - Qt C++ - 在 QSplashScreen 中对齐 QProgressBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12682888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com