QMessagebox for IOS开发的任何替代解决方案(仅限QWidget应用程序)?

1 qt ios qt5

我正在使用Qt 5.3并尝试为IOS开发应用程序.

问题是,iPhone Retina模拟器中的QWidget应用程序:

  1. QMessage变为全屏.
  2. 在Application输出面板中,我看到:此插件不支持propagateSizeHints().

所以寻找QMessageBox的替代解决方案.我还不想学习QML.

phy*_*att 5

如果您在窗口小部件上执行叠加,则可以创建类似于iOS弹出窗口的内容.

基本上,您创建了另一个窗口小部件,并将其父级添加到您希望在其上绘制的窗口小部件.

这里有一些有用的标志和代码行放在你的覆盖构造函数中:

setPalette(Qt::transparent);
// if you have buttons on this overlay you probably don't want this one
setAttribute(Qt::WA_TransparentForMouseEvents);

QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
dse->setBlurRadius(20);
this->setGraphicsEffect(dse);
Run Code Online (Sandbox Code Playgroud)

然后确保在父窗口小部件调整大小时命令调整叠加层的大小:

void ParentWidget::resizeEvent(QResizeEvent *event)
{
    overlay->resize(event->size());
    event->accept();
}
Run Code Online (Sandbox Code Playgroud)

http://www.qtcentre.org/wiki/index.php?title=Widget_Overlay

更新:很棒的例子

覆盖消息框的屏幕截图

main.cpp中

#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.resize(300,600);

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "overlaydialogbox.h"
#include <QResizeEvent>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void resizeEvent(QResizeEvent *event);

private:
    OverlayDialogBox * m_overlay;
};

#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    m_overlay = new OverlayDialogBox(this);
}

MainWindow::~MainWindow() { }

void MainWindow::resizeEvent(QResizeEvent *event)
{
    m_overlay->resize(event->size());
    event->accept();
}
Run Code Online (Sandbox Code Playgroud)

overlaydialogbox.h

#ifndef OVERLAYDIALOGBOX_H
#define OVERLAYDIALOGBOX_H

#include <QWidget>

class OverlayDialogBox : public QWidget
{
    Q_OBJECT
public:
    explicit OverlayDialogBox(QWidget *parent = 0);

signals:
    void accepted();
    void rejected();
    void finished(int);
public slots:
};

#endif // OVERLAYDIALOGBOX_H
Run Code Online (Sandbox Code Playgroud)

overlaydialogbox.cpp

#include "overlaydialogbox.h"
#include <QGridLayout>
#include <QGraphicsEffect>
#include <QLabel>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QIcon>

OverlayDialogBox::OverlayDialogBox(QWidget *parent) :
    QWidget(parent)
{
    setPalette(Qt::transparent);
    // if you have buttons on this overlay you probably don't want this one
//    setAttribute(Qt::WA_TransparentForMouseEvents);

    QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
    dse->setBlurRadius(20);
    this->setGraphicsEffect(dse);

    QGridLayout * grid = new QGridLayout();
    this->setLayout(grid);

    QMessageBox * msg = new QMessageBox(QMessageBox::Warning,"Testing","This is a test QMessageBox.");
    QObject::connect(msg, SIGNAL(accepted()), this, SIGNAL(accepted()));
    QObject::connect(msg, SIGNAL(finished(int)), this, SIGNAL(finished(int)));
    QObject::connect(msg, SIGNAL(rejected()), this, SIGNAL(rejected()));
    QObject::connect(msg, SIGNAL(finished(int)), this, SLOT(close()));

    msg->setPalette(Qt::white);

    grid->addWidget(msg);
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.