Any alternative solution for QMessagebox for IOS development (QWidget application only)?

后端 未结 1 1250
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 00:51

I am using Qt 5.3 and trying to develop application for IOS.

Problem is, QWidget application in a iPhone Retina simulator:

  1. QMessage becomes full-screen
相关标签:
1条回答
  • 2021-01-16 01:09

    If you do an overlay on top of your widget you can make something similar to the iOS popups.

    Basically you create another widget, and you parent it to the widget you want it to be drawn on top of.

    Here are some helpful flags and lines of code to put in your overlay constructor:

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

    Then be sure to command a resize of your overlay when the parent widget resizes:

    void ParentWidget::resizeEvent(QResizeEvent *event)
    {
        overlay->resize(event->size());
        event->accept();
    }
    

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

    UPDATE: Awesome example

    screenshot of overlay messagebox

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

    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
    

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

    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
    

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

    Hope that helps.

    0 讨论(0)
提交回复
热议问题