5

QSplashScreen をサブクラス化して a のQProgressBar中に入れています。QSplashScreenメソッドをオーバーライドしますdrawContents()

ジオメトリを正しく設定したと思っていましたが、画面の上部と下部の両方にレンダリングされます。どうしてか分かりません。合わせ方は他にもあるのではないでしょうか。画像は 380x284 であるため、数値は正しいため、高さ 19 のプログレス バーは 265 ピクセル下になるはずです。

印刷画面ボタンでスプラッシュ スクリーンが表示されませんでした。現時点では 1 色の白いスプラッシュ スクリーンですが、ご覧のとおり、上下にプログレス バーがあります (どちらも同じ色で、カメラからの照明です)。

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

もう 1 つの問題はshowMessage()、QSplashScreen のメソッドです。メッセージをプログレスバーの上に右揃えで表示したい...誰かがそれを行う方法を知っているなら。

スプラッシュスクリーン.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

main.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();
}
4

1 に答える 1

5

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);
}

これはあなたを助けるかもしれません。

于 2012-10-02T07:35:28.657 に答える