2017-03-15 2 views
1

My Qt 애플리케이션은 QStackedLayout()에 추가 된 여러 개의 화면으로 구성됩니다. 이제 몇 가지 사용자 작업을 한 후 몇 초 후에 작업을 확인하고 사라지는 작은 팝업 창이 나타납니다. 내가 원하는 것은 검은 색 테두리와 텍스트가있는 회색 직사각형입니다. 버튼 없음, 제목 표시 줄 없음.Qt에서 사용자 정의 팝업 창을 프로그래밍하는 방법은 무엇입니까?

QMessage Box (아래 코드 참조)로 처리하려고했지만 일반적으로 QMessageBox()의 테두리 스타일을 조정할 수없는 것 같습니다. 또한 크기는 조정할 수 없습니다.

QMessageBox* tempbox = new QMessageBox; 
tempbox->setWindowFlags(Qt::FramelessWindowHint); //removes titlebar 
tempbox->setStandardButtons(0); //removes button 
tempbox->setText("Some text"); 
tempbox->setFixedSize(800,300); //has no effect 
tempbox->show(); 
QTimer::singleShot(2000, tempbox, SLOT(close())); //closes box after 2 seconds 

Qt에서 맞춤 팝업 창을 프로그래밍하려면 어떻게해야합니까?

+0

*** 크기를 조정할 수 없습니다. *** 현재 응용 프로그램 중 하나에서 긴 메시지를 표시하도록 메시지 상자의 너비를 설정했기 때문에 이것이 잘못되었다는 것을 알고 있습니다. 나중에 구현을 확인해야 할 것입니다. – drescherjm

답변

3

우선 Qt 문서의 Windows Flags Example을 권하고 싶습니다. 이 주제로 놀기 좋은 샘플을 제공합니다. 이 샘플에서는 QWidget을 파생시켜 고유 플래그의 효과를 보여줍니다. 적절한 Qt::WindowFlags이 설정되면 QWidget을 사용할 수 있다는 생각이 들었습니다. 나는

  • 때문에 텍스트
  • 이 때문에, 프레임을 가질 수 QFrame에서 상속을 표시 할 수 있습니다 QLabel을 choosed 할.

소스 코드 testQPopup.cc : 윈도우 10 (64 비트)에 VS2013와 Qt를 5.6로 컴파일

// standard C++ header: 
#include <iostream> 
#include <string> 

// Qt header: 
#include <QApplication> 
#include <QLabel> 
#include <QMainWindow> 
#include <QTimer> 

using namespace std; 

int main(int argc, char **argv) 
{ 
    cout << QT_VERSION_STR << endl; 
    // main application 
#undef qApp // undef macro qApp out of the way 
    QApplication qApp(argc, argv); 
    // setup GUI 
    QMainWindow qWin; 
    qWin.setFixedSize(640, 400); 
    qWin.show(); 
    // setup popup 
    QLabel qPopup(QString::fromLatin1("Some text"), 
    &qWin, 
    Qt::SplashScreen | Qt::WindowStaysOnTopHint); 
    QPalette qPalette = qPopup.palette(); 
    qPalette.setBrush(QPalette::Background, QColor(0xff, 0xe0, 0xc0)); 
    qPopup.setPalette(qPalette); 
    qPopup.setFrameStyle(QLabel::Raised | QLabel::Panel); 
    qPopup.setAlignment(Qt::AlignCenter); 
    qPopup.setFixedSize(320, 200); 
    qPopup.show(); 
    // setup timer 
    QTimer::singleShot(1000, 
    [&qPopup]() { 
    qPopup.setText(QString::fromLatin1("Closing in 3 s")); 
    }); 
    QTimer::singleShot(2000, 
    [&qPopup]() { 
    qPopup.setText(QString::fromLatin1("Closing in 2 s")); 
    }); 
    QTimer::singleShot(3000, 
    [&qPopup]() { 
    qPopup.setText(QString::fromLatin1("Closing in 1 s")); 
    }); 
    QTimer::singleShot(4000, &qPopup, &QLabel::hide); 
    // run application 
    return qApp.exec(); 
} 

I. 아래의 이미지는의 스냅 샷을 보여줍니다

Snapshot of testQPopup.exe

팝업 더 잘 볼 수 있도록하려면을 (내가 그것을 좋아하기 때문에), 나는 팝업에 대한 QLabel의 배경색을 변경했습니다. 그리고, 나는 카운트 다운을 약간 추가하는 것에 저항 할 수 없었다.

+0

멋진 작품! 감사합니다 보스;) –

관련 문제