1. 程式人生 > >qt學習之各種對話方塊的實現

qt學習之各種對話方塊的實現

1 目的:實現1 顏色對話方塊

2  實現顏色標準對話方塊

3 實現字型標準對話方塊

4 實現標準輸入對話方塊

5 實現標準訊息對話方塊

6 實現自定義對話方塊

 程式碼實現

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QPushButton>
#include <QLineEdit>
#include <QFrame>
#include "inputdialog.h"
#include "messagedialog.h"
#include "customdialog.h"
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:
    QFileDialog *FileDialog;
    QColorDialog *ColorDialog;
    QFontDialog *FontDialog;
    InputDialog *MInputDialog;
    QMessageBox *MessageBox;
    QPushButton *FilePushButton;
    QPushButton *ColorPushButton;
    QPushButton *FontPushButton;
    QPushButton *InputButton;
    QPushButton *MegButton;
    QPushButton *CustomButton;
    QLineEdit *FileLineEdit;
    QFrame *ColorFrame;
    QLineEdit *FontEditDialog;
    MessageDialog *MyMessageDialog;
private slots:
    void showFileDialog();
    void showColorDialog();
    void showFontDialog();
    void showInputDialog();
    void showMsgDialog();
    void showCustomDialog();
};

#endif // DIALOG_H
customdialog.h
#ifndefCUSTOMDIALOG_H
#defineCUSTOMDIALOG_H
#include<QLabel>
#include<QFrame>
#include<QPicture>
#include<QPushButton>
#include<QDialog>
classCustomDialog:publicQDialog
{
Q_OBJECT
public:
CustomDialog(QWidget*parent=0);
private:
QLabel*label;
QPushButton
*YesButton;
QPushButton*NoButton;
QPushButton*CancelButton;
QFrame*photoFrame;
QPicture*photoPicture;
QLabel*photoLabel;
};
#endif//CUSTOMDIALOG_H
inputdialog.h
#ifndefINPUTDIALOG_H
#defineINPUTDIALOG_H
#include<QDialog>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QInputDialog>
#include<QLabel>
classInputDialog:publicQDialog
{
Q_OBJECT
public:
InputDialog(QWidget*parent=0);
private:
QLabel*NameLabel;
QLabel*SexLabel;
QLabel*AgeLabel;
QLabel*GreadLabel;
QLabel*DNameLabel;
QLabel*DAgeLabel;
QLabel*DSexLabel;
QLabel*DGreadLabel;
QPushButton*NameButton;
QPushButton*SexButton;
QPushButton*AgeButton;
QPushButton*GreadButton;
privateslots:
voidalterName();
voidalterSex();
voidalterAge();
voidalterGread();
};
#endif//INPUTDIALOG_H
messagedialog.h
#ifndefMESSAGEDIALOG_H
#defineMESSAGEDIALOG_H
#include<QDialog>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QInputDialog>
#include<QLabel>
classMessageDialog:publicQDialog
{
Q_OBJECT
public:
MessageDialog(QWidget*parent=0);
private:
QLabel*SelMsgBox;
QPushButton*QuestionMsg;
QPushButton*WarningMsg;
QPushButton*AboutMsg;
QPushButton*InformationMsg;
QPushButton*CriticalMsg;
QPushButton*AboutQtMsg;
privateslots:
voidshowQuestionMsg();
voidshowWarningMsg();
voidshowAboutMsg();
voidshowInformationMsg();
voidshowCriticalMsg();
voidshowAboutQtMsg();
};
#endif//MESSAGEDIALOG_H
customdialog.cpp
#include"customdialog.h"
#include<QDialog>
#include<QHBoxLayout>
#include<QGridLayout>
#include<QMessageBox>
CustomDialog::CustomDialog(QWidget*parent)
:QDialog(parent)
{
label=newQLabel("thisCustomMsgBox!");
YesButton=newQPushButton("YES");
NoButton=newQPushButton("NO");
CancelButton=newQPushButton("CANCEL");
photoLabel=newQLabel;
this->setWindowTitle("CustomMsgBox");
photoLabel->setPixmap(QPixmap("qt.jpg"));
QHBoxLayout*bottonLayout=newQHBoxLayout;
bottonLayout->addWidget(YesButton,0);
bottonLayout->addWidget(NoButton,1);
bottonLayout->addWidget(CancelButton,2);
QGridLayout*MainLayout=newQGridLayout(this);
MainLayout->addWidget(photoLabel,0,0);
MainLayout->addWidget(label,0,1);
MainLayout->addLayout(bottonLayout,1,0,1,1);
}
dialog.cpp
#include"dialog.h"
#include<QGridLayout>
#include<QFont>
Dialog::Dialog(QWidget*parent)
:QDialog(parent)
{
FileDialog=newQFileDialog(this,"openfiledialog","/","yangsen(*.*)");//最後如果有多個則使用::比如:C++Files(*.c++)::CFiles(*.c)::HeadFiles(*.h)
ColorDialog=newQColorDialog;
FontDialog=newQFontDialog;
//InputDialog=newQInputDialog;
MessageBox=newQMessageBox;
ColorFrame=newQFrame;
ColorFrame->setFrameShape(QFrame::Box);//設定邊框
ColorFrame->setAutoFillBackground(true);//設定顏色自動更新
//字型
FontEditDialog=newQLineEdit("Welcome");
FilePushButton=newQPushButton(QStringLiteral("檔案標準對話方塊例項"));
ColorPushButton=newQPushButton(QStringLiteral("顏色標準對話方塊例項"));
FontPushButton=newQPushButton(QStringLiteral("字型標準對話方塊例項"));
InputButton=newQPushButton(QStringLiteral("標準輸入對話方塊例項"));
MegButton=newQPushButton(QStringLiteral("標準訊息對話方塊例項"));
CustomButton=newQPushButton(QStringLiteral("自定義訊息對話方塊"));
FileLineEdit=newQLineEdit;
MInputDialog=newInputDialog();
MyMessageDialog=newMessageDialog();
QGridLayout*MainLayout=newQGridLayout(this);
MainLayout->addWidget(FilePushButton,0,0);
MainLayout->addWidget(ColorPushButton,1,0);
MainLayout->addWidget(FontPushButton,2,0);
MainLayout->addWidget(InputButton,3,0);
MainLayout->addWidget(MegButton,3,1);
MainLayout->addWidget(CustomButton,4,0);
MainLayout->addWidget(FileLineEdit,0,1);
MainLayout->addWidget(ColorFrame,1,1);
MainLayout->addWidget(FontEditDialog,2,1);
//MainLayout->addWidget(
connect(FilePushButton,SIGNAL(clicked()),this,SLOT(showFileDialog()));
connect(ColorPushButton,SIGNAL(clicked()),this,SLOT(showColorDialog()));
connect(FontPushButton,SIGNAL(clicked()),this,SLOT(showFontDialog()));
connect(InputButton,SIGNAL(clicked()),this,SLOT(showInputDialog()));
connect(MegButton,SIGNAL(clicked()),this,SLOT(showMsgDialog()));
connect(CustomButton,SIGNAL(clicked()),this,SLOT(showCustomDialog()));
}
voidDialog::showFileDialog()
{
this->FileLineEdit->setText(FileDialog->getOpenFileName());
}
voidDialog::showColorDialog()
{
QColorcolor=this->ColorDialog->getColor();
if(color.isValid())
{
this->ColorFrame->setPalette(QPalette(color));
}
}
voidDialog::showFontDialog()
{
boolok;
QFontfont=this->FontDialog->getFont(&ok);
if(ok)
{
this->FontEditDialog->setFont(font);
}
}
voidDialog::showInputDialog()
{
//QStringstr=this->InputDialog->getText(this,"InputDialog","Input:");
//QMessageBox::question(this,"Mesagebox",str,QMessageBox::NoButton);
this->MInputDialog->show();
}
voidDialog::showMsgDialog()
{
this->MyMessageDialog->show();
}
voidDialog::showCustomDialog()
{
CustomDialog*customDialog=newCustomDialog;
customDialog->show();
}
Dialog::~Dialog()
{
}
inputdialog.cpp
#include"inputdialog.h"
#include<QGridLayout>
#include<QInputDialog>
#include<QDebug>
InputDialog::InputDialog(QWidget*parent)
:QDialog(parent)
{
NameLabel=newQLabel(QStringLiteral("姓名"));
SexLabel=newQLabel(QStringLiteral("性別"));
AgeLabel=newQLabel(QStringLiteral("年齡"));
GreadLabel=newQLabel(QStringLiteral("成績"));
DNameLabel=newQLabel;
DAgeLabel=newQLabel;
DSexLabel=newQLabel;
DGreadLabel=newQLabel;
DNameLabel->setFrameStyle(QFrame::Sunken|QFrame::Panel);
DAgeLabel->setFrameStyle(QFrame::Sunken|QFrame::Panel);
DSexLabel->setFrameStyle(QFrame::Sunken|QFrame::Panel);
DGreadLabel->setFrameStyle(QFrame::Sunken|QFrame::Panel);
NameButton=newQPushButton(QStringLiteral("修改姓名"));
SexButton=newQPushButton(QStringLiteral("修改性別"));
AgeButton=newQPushButton(QStringLiteral("修改年齡"));
GreadButton=newQPushButton(QStringLiteral("修改成績"));
QGridLayout*MainLayout=newQGridLayout(this);
MainLayout->addWidget(NameLabel,0,0);
MainLayout->addWidget(SexLabel,1,0);
MainLayout->addWidget(AgeLabel,2,0);
MainLayout->addWidget(GreadLabel,3,0);
MainLayout->addWidget(DNameLabel,0,1);
MainLayout->addWidget(DSexLabel,1,1);
MainLayout->addWidget(DAgeLabel,2,1);
MainLayout->addWidget(DGreadLabel,3,1);
MainLayout->addWidget(NameButton,0,2);
MainLayout->addWidget(SexButton,1,2);
MainLayout->addWidget(AgeButton,2,2);
MainLayout->addWidget(GreadButton,3,2);
connect(NameButton,SIGNAL(clicked()),this,SLOT(alterName()));
connect(SexButton,SIGNAL(clicked()),this,SLOT(alterSex()));
connect(AgeButton,SIGNAL(clicked()),this,SLOT(alterAge()));
connect(GreadButton,SIGNAL(clicked()),this,SLOT(alterGread()));
}
voidInputDialog::alterName()
{
this->DNameLabel->setText(QInputDialog::getText(NULL,"InputTextDialog","Name:"));
}
voidInputDialog::alterSex()
{
QStringListSexList;
SexList<<"man"<<"woman";
this->DSexLabel->setText(QInputDialog::getItem(NULL,"ComboxDialog","SEX:",SexList,0,false));
}
voidInputDialog::alterAge()
{
intage=QInputDialog::getInt(NULL,"IntDialog","Age:",0,0,200);
QStringstrAge;
//strAge=QString("%1").arg(age);
//strAge
strAge.sprintf("%d\0",age);
this->DAgeLabel->setText(strAge);
}
voidInputDialog::alterGread()
{
floatgread=QInputDialog::getDouble(NULL,"FloatDialog","Gread:",0);
QStringstrGread;
strGread.sprintf("%f\0",gread);
this->DGreadLabel->setText(strGread);
}
messagedialog.cpp
#include"messagedialog.h"
#include<QDialog>
#include<QMessageBox>
#include<QGridLayout>
MessageDialog::MessageDialog(QWidget*parent)
:QDialog(parent)
{
SelMsgBox=newQLabel("list:");
QuestionMsg=newQPushButton("QuestionMsg");
WarningMsg=newQPushButton("WarningMsg");
AboutMsg=newQPushButton("AboutMsg");
InformationMsg=newQPushButton("InformationMsg");
CriticalMsg=newQPushButton("CriticalMsg");
AboutQtMsg=newQPushButton("AboutQtMsg");
QGridLayout*MainLayout=newQGridLayout(this);
MainLayout->addWidget(SelMsgBox,0,0,0,1);
MainLayout->addWidget(QuestionMsg,1,0);
MainLayout->addWidget(WarningMsg,1,1);
MainLayout->addWidget(AboutMsg,2,0);
MainLayout->addWidget(InformationMsg,2,1);
MainLayout->addWidget(CriticalMsg,3,0);
MainLayout->addWidget(AboutQtMsg,3,1);
connect(QuestionMsg,SIGNAL(clicked()),this,SLOT(showQuestionMsg()));
connect(WarningMsg,SIGNAL(clicked()),this,SLOT(showWarningMsg()));
connect(AboutMsg,SIGNAL(clicked()),this,SLOT(showAboutMsg()));
connect(InformationMsg,SIGNAL(clicked()),this,SLOT(showInformationMsg()));
connect(CriticalMsg,SIGNAL(clicked()),this,SLOT(showCriticalMsg()));
connect(AboutQtMsg,SIGNAL(clicked()),this,SLOT(showAboutQtMsg()));
}
voidMessageDialog::showQuestionMsg()
{
QMessageBox::question(NULL,"QuestionMsgBox","youarepis?",QMessageBox::Yes,QMessageBox::No);
}
voidMessageDialog::showWarningMsg()
{
QMessageBox::warning(NULL,"WarningMsgBox","therehavepig!",QMessageBox::Yes);
}
voidMessageDialog::showAboutMsg()
{
QMessageBox::about(NULL,"AboutMsgBox","iamsuperman!");
}
voidMessageDialog::showInformationMsg()
{
QMessageBox::information(NULL,"InforMationMsgBox","thisInformationMsgBox");
}
voidMessageDialog::showCriticalMsg()
{
QMessageBox::critical(NULL,"CriticalMsgBox","thisisCriticalmsgbox!",QMessageBox::Yes,QMessageBox::No);
}
voidMessageDialog::showAboutQtMsg()
{
QMessageBox::aboutQt(NULL);
}
遇到問題有

1 漢字編碼問題,試了各種方法依舊不能解決最後使用

QStringLiteral("檔案標準對話方塊例項")解決了
2 圖片載入問題,用360瀏覽器下載了一張圖片始終不能載入,後面發現是圖片問題,圖片需要到畫圖裡面進行開啟再另存為才行.

相關推薦

qt學習各種對話方塊實現

1 目的:實現1 顏色對話方塊 2  實現顏色標準對話方塊 3 實現字型標準對話方塊 4 實現標準輸入對話方塊 5 實現標準訊息對話方塊 6 實現自定義對話方塊  程式碼實現 dialog.h #ifndef DIALOG_H #define DIALOG_H #incl

Qt學習如何用程式碼實現UI佈局及顯示

本文以Qtableview為例進行說明。 Qtableview是一個表格類,可以使用這個類來設定表格,進行內容的顯示、增刪、查詢等等。 首先:正常建立一個Qt Widget Application專案。 mainwindow.h public: explicit MainWi

MFC程式設計學習改變對話方塊和控制元件及文字顏色

//程式訊息流:首先對話方塊繪製時,會發送WM_CTLCOLOR訊息,那麼OnCtlColor()函式 //將響應該訊息,程式將用該函式返回的畫刷對控制元件進行繪製。 HBRUSH CSettingDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtl

Qt學習路(7): 建立一個對話方塊(上)

#ifndef FINDDIALOG_H #define FINDDIALOG_H  #include <QtGui/QDialog>  class QCheckBox; class QLabel; class QLineEdit; class QPushButton;  class FindD

Qt學習路12--Qt標準對話方塊(字型對話方塊、進度條對話方塊和列印對話方塊

使用預定義對話方塊的三板斧原則:定義物件後設置父元件和屬性、模態呼叫exec()、根據結果判斷執行流程。 字型對話方塊 字型對話方塊屬於Qt預定義的對話方塊型別,它的作用就是通過使用者得到字型型別並返回。 QFontDialog dia(this)

QT學習 對話方塊 (四) 字型對話方塊、訊息對話方塊、檔案對話方塊、程序對話方塊

QMessageBox類: 含有Question訊息框、Information訊息框、Warning訊息框和Critical訊息框等 通常有兩種方式可以來建立標準訊息對話方塊: 一種是採用“基於屬性”的API,一種是使用QMessageBox的靜態方法。 後者書寫容易,但缺

QT學習 對話方塊 (一) 可擴充套件對話方塊

QT中的對話方塊是QDialog類 下面是三個主要的視窗的區別: QWidget類是所有使用者介面物件的基類。 視窗部件是使用者介面的一個基本單元:它從視窗系統接收滑鼠、鍵盤和其它事件,並且在螢幕上繪製自己。每一個視窗部件都是矩形的,並且它們按Z軸順序排列。一個視窗部件可以

Qt標準對話方塊(字型對話方塊

字型對話方塊 1、包含標頭檔案 #include < QFontDialog> void Widget::on_FontButton_clicked() { //ok 用

Qt 學習之路 2(19):事件的接受與忽略(當重寫事件回撥函式時,時刻注意是否需要通過呼叫父類的同名函式來確保原有實現仍能進行!有好幾個例子。為什麼要這麼做?而不是自己去手動呼叫這兩個函式呢?因為我們無法確認父類中的這個處理函式有沒有額外的操作)

版本: 2012-09-29 2013-04-23 更新有關accept()和ignore()函式的相關內容。 2013-12-02 增加有關accept()和ignore()函式的示例。 上一章我們介紹了有關事件的相關內容。我們曾經提到,事件可以依情況接受和忽略。現在,我們就

MFC學習筆記對話方塊中使用定時器

MFC菜鳥第一次發貼,請各位高人指教:我需要做一個基於對話方塊的程式,主對話方塊TimerTryDlg上就兩個按鈕,確定鈕按下後執行訊息處理OnFirstOK(),先初始化定時器,再開啟有模式對話方塊TestDlg,定時器時間到了執行OnTimer-在子對話方塊上的一個靜態控制元件上畫點,但OnTimer在

Qt標準對話方塊(檔案對話方塊

檔案對話方塊 目錄 檔案對話方塊(單檔案) 1、包含標頭檔案 #include< QFileDialog> void Widget::on_FileButton

QT學習路十六(QWebView實現簡易瀏覽器)

QtWebkit 模組介紹   QtWebkit 模組提供了一個在Qt中使用web browser的engine,這使得我們在QT的應用程式中使用全球資訊網上的內容變得很容易,而且對其網頁內容的控制也可以通過native controls 實現  。   QtWebkit具

QT選擇檔案對話方塊實現多種型別檔案匹配過濾

這個功能確實不知道如何說,就是說在選擇檔案的時候是可以篩選的,但是我有時候要篩選的是音樂,有時候是圖片,這個時候就要用到這個功能了。 其實也只是很簡單的程式碼,可是很實用。程式碼如下: QString

Qt標準對話方塊(顏色對話方塊)

顏色對話方塊 目錄 顏色對話方塊 1、在標頭檔案中包含 #include < QColorDialog > void Widget::on_ColorButton_clicked() { QColor col

Qt 關於彈出對話方塊實現居中顯示!

本人實現的是 QFrame 居中! 因為要實現QFrame 彈出居中顯示。。 在QFrame->show之前實現move,但是這樣是取不到QFrame真實的大小! 在QFrame->show之後實現move,這時move就會出現閃屏的現象發生。 用resi

QT學習forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget"

文件 exceptio 錯誤 執行 類名 nbsp dia dial truct 解決QT:forward declaration of ‘struct Ui::xxx‘;invalid use of incomplete struct "Ui::Widget" 等莫名奇妙

Qt 學習路 2(40):隱式數據共享

深拷貝和淺拷貝 != 這樣的 pointer map painter pos 轉載 多線程 博客轉載自:https://www.devbean.net/2013/01/qt-study-road-2-implicit-sharing/ Qt 中許多 C++ 類使用了隱式數據

ES源碼學習--Get API的實現邏輯

ive 應用 pretty 合並 pri cut ring etc normalize Github上es項目講述其易用性時,用來舉例說明ES開箱即用的特性,用的就是Get API。片段摘取如下: -- 添加文檔 curl -XPUT ‘http://localhost:9

Koa2學習旅----結合 jqPaginator實現列表分頁

1.下載jqPaginator.js  放在靜態目錄裡面 2、在需要分頁的地方引入jqPaginator.js  3、在需要分頁的地方加個空div     <div id="page" class="pagination"></

python學習元組物件實現機制解析

myTuple=(1,2,3,4) id(myTuple[0]) 1652911120 a=1 id(a) 1652911120 元組第一位元素的地址和整形變數a的地址是一樣的,說明他們都指向常量1所在的地址空間,常