zllx1314的个人主页

http://www.qtcn.org/bbs/u/199420  [收藏] [复制]

zllx1314

  • 0

    关注

  • 0

    粉丝

  • 0

    访客

  • 等级:新手上路
  • 总积分:2
  • 男,1988-02-22

最后登录:2020-03-26

更多资料

日志

Qt5开发及实例 标准对话框

2020-03-14 19:29

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLineEdit>
#include <QGridLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QFrame>
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();
public:
    void init();
private slots:
    void showFile();
    void showColor();
    void showFont();
private:
    //file
    QPushButton *Filebtn;
    QLineEdit *FileLineEdit;
    QGridLayout *mainLayout;

    //color
    QPushButton *Colorbtn;
    QFrame *ColorFrame;

    //font
    QPushButton *FontBtn;
    QLineEdit *FontLineEdit;
};
#endif // DIALOG_H
#include "dialog.h"
#include <QColor>
#include <QColorDialog>
#include <QPalette>
#include <QFont>
#include <QFontDialog>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("各种标准对话框实例"));
    init();
    connect(Filebtn,SIGNAL(clicked()),this,SLOT(showFile()));
    connect(Colorbtn,SIGNAL(clicked()),this,SLOT(showColor()));
    connect(FontBtn,SIGNAL(clicked()),this,SLOT(showFont()));
}

Dialog::~Dialog()
{
}

void Dialog::showFile()
{
    QString s = QFileDialog::getOpenFileName(this,"Open file dialog","/","C++ file(*.cpp);;C file(*.c);;Head files(*.h)");
    FileLineEdit->setText(s);
}

void Dialog::showColor()
{
    QColor c = QColorDialog::getColor(Qt::blue);
    if (c.isValid())
    {
        ColorFrame->setPalette(QPalette(c));
    }
}

void Dialog::showFont()
{
   bool ok;
   QFont f = QFontDialog::getFont(&ok);
   if (ok)
   {
       FontLineEdit->setFont(f);
   }
}

void Dialog::init()
{
    Filebtn = new QPushButton(tr("文件标准对话框实例"));
    FileLineEdit = new QLineEdit;

    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(Filebtn,0,0);
    mainLayout->addWidget(FileLineEdit,0,1);

    Colorbtn = new QPushButton(tr("颜色标准对话框实例"));
    ColorFrame = new QFrame;
    ColorFrame->setFrameShape(QFrame::Box);
    ColorFrame->setAutoFillBackground(true);

    mainLayout->addWidget(Colorbtn,1,0);
    mainLayout->addWidget(ColorFrame,1,1);

    FontBtn = new QPushButton(tr("字体标准对话框"));
    FontLineEdit = new QLineEdit;

    FontLineEdit->setText("Welcome");

    mainLayout->addWidget(FontBtn,2,0);
    mainLayout->addWidget(FontLineEdit,2,1);


}
#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}
分类:qt学习|回复:0|浏览:73|全站可见|转载
 

下一篇:

上一篇:

Powered by phpwind v8.7 Certificate Copyright Time now is:03-31 07:11
©2005-2016 QTCN开发网 版权所有 Gzip disabled