Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3474
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-20 15:26
文章分类
文章存档

2015年(1)

我的朋友
最近访客

分类: LINUX

2015-04-20 15:28:08

原文地址:[Qt]标准对话框 作者:ruglcc


standdlg.h

点击(此处)折叠或打开

  1. #ifndef STANDARDDIALOGS_H
  2. #define STANDARDDIALOGS_H

  3. #include <QtGui>

  4. class StandardDialogs : public QDialog
  5. {
  6.     Q_OBJECT
  7. public:
  8.     StandardDialogs( QWidget *parent=0, Qt::WindowFlags f=0 );
  9.     ~StandardDialogs();
  10. public:
  11.     QGridLayout *layout;
  12.     
  13.     QPushButton *filePushButton;
  14.     QPushButton *colorPushButton;
  15.     QPushButton *fontPushButton;

  16.     QLineEdit *fileLineEdit;
  17.     QLineEdit *fontLineEdit;    
  18.     QFrame *colorFrame;
  19.     
  20. private slots:
  21.     void slotOpenFileDlg();
  22.     void slotOpenColorDlg();
  23.     void slotOpenFontDlg();

  24. };


  25. #endif

standarddlg.cpp

点击(此处)折叠或打开

  1. #include "standarddialogs.h"

  2. StandardDialogs::StandardDialogs( QWidget *parent, Qt::WindowFlags f )
  3.     : QDialog( parent, f )
  4. {
  5.     setWindowTitle(tr("Standard Dialogs"));

  6.     layout = new QGridLayout( this );
  7.     
  8.     filePushButton = new QPushButton();
  9.     filePushButton->setText(tr("File Dialog"));
  10.     
  11.     colorPushButton = new QPushButton();
  12.     colorPushButton->setText(tr("Color Dialog"));
  13.     
  14.     fontPushButton = new QPushButton( );
  15.     fontPushButton->setText(tr("Font Dialog"));

  16.     fileLineEdit = new QLineEdit();
  17.     
  18.     colorFrame = new QFrame();
  19.     colorFrame->setFrameShape( QFrame::Box );
  20.     colorFrame->setAutoFillBackground(true);
  21.     
  22.     fontLineEdit = new QLineEdit( );
  23.     fontLineEdit->setText(tr("Hello World"));
  24.         
  25.     layout->addWidget( filePushButton, 0, 0 );    
  26.     layout->addWidget( fileLineEdit, 0, 1 );
  27.     layout->addWidget( colorPushButton, 1, 0 );
  28.     layout->addWidget( colorFrame, 1, 1 );
  29.     layout->addWidget( fontPushButton, 2, 0 );
  30.     layout->addWidget( fontLineEdit, 2, 1 );
  31.     layout->setMargin(15);
  32.     layout->setSpacing(10);
  33.     
  34.     connect(filePushButton,SIGNAL(clicked()),this,SLOT(slotOpenFileDlg()));
  35.     connect(colorPushButton,SIGNAL(clicked()),this,SLOT(slotOpenColorDlg()));
  36.     connect(fontPushButton,SIGNAL(clicked()),this,SLOT(slotOpenFontDlg()));
  37. }

  38. StandardDialogs::~StandardDialogs()
  39. {
  40. }
  41.                               
  42. void StandardDialogs::slotOpenFileDlg()
  43. {
  44.     QString s = QFileDialog::getOpenFileName(
  45.     this, "open file dialog",
  46.         "/",
  47.         "C++ files (*.cpp);;C files (*.c);;Head files (*.h)");
  48.                                     
  49.     fileLineEdit->setText( s.toAscii() );
  50. }
  51.                                                             
  52. void StandardDialogs::slotOpenColorDlg()
  53. {
  54.     QColor color = QColorDialog::getColor (Qt::blue);
  55.     
  56.     if(color.isValid())
  57.     {
  58.         colorFrame->setPalette(QPalette(color));
  59.     }
  60. }
  61.                                                             
  62. void StandardDialogs::slotOpenFontDlg()
  63. {
  64.     bool ok;
  65.                               
  66.     QFont font = QFontDialog::getFont( &ok );
  67.     
  68.     if( ok )
  69.     {
  70.         fontLineEdit->setFont( font );
  71.     }

  72. }

main.cpp

点击(此处)折叠或打开

  1. #include "standarddialogs.h"
  2. #include <QApplication>

  3. int main( int argc, char **argv )
  4. {
  5.     QFont font("ZYSong18030",12);
  6.     QApplication::setFont(font);
  7.         
  8.     QApplication a( argc, argv );
  9.     QTranslator translator(0);
  10.     translator.load("standarddialogs_zh",".");
  11.     a.installTranslator(&translator);
  12.     
  13.     StandardDialogs *standarddialogs = new StandardDialogs();
  14.     standarddialogs->show();
  15.     return a.exec();
  16. }




程序效果

image

 

image

 

image

image

阅读(539) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~