來源: http://blog.csdn.net/a2604539133/article/details/73920696程序員
#include <QtWidgets/QApplication> #include <QtCore/qdebug.h> #include <QtWidgets/qcompleter.h> #include <QtWidgets/qlineedit.h> #include <QtWidgets/qlabel.h> #include <QtWidgets/qcombobox.h> #include <QtWidgets/qcheckbox.h> #include <QtWidgets/qradiobutton.h> #include <QtWidgets/qtextedit.h> #include <QtWidgets/qtextbrowser.h> #include <QtWidgets/qgroupbox.h> #include <QtWidgets/qslider.h> #include <QtWidgets/qspinbox.h> #include <QtWidgets/qdatetimeedit.h> #include <QtWidgets/qtabwidget.h> #include <QtWidgets/qlcdnumber.h> #include <QtGui/qpixmap.h> #include <QtCore/qobject.h> #include <QtWidgets/qboxlayout.h> #include <QtWidgets/qpushbutton.h> #include "MyWidgetEvent.h" void test() ; int main(int argc, char *argv[]) { QApplication a(argc, argv); test() ; return a.exec(); } void test(){ } void test5ManyKongJian() { QWidget *nanWidget = new QWidget() ; QVBoxLayout *nanVLayout = new QVBoxLayout() ; /* **測試label控件 */ QLabel *label=nullptr ; nanVLayout->addWidget( label = new QLabel("<a href=www.baidu.com>baidu</a>") ) ; //QPixmap me("./me.png") ; //label->setPixmap( me ) ;//問題:連接和圖片重複了,怎麼分開 label->setWordWrap( true ) ; label->adjustSize() ; nanVLayout->connect( label , &QLabel::linkActivated , []( QString str){ qDebug()<<str ; } ) ; /* **測試lineedit控件 */ QLineEdit *lineEdit ; nanVLayout->addWidget( lineEdit = new QLineEdit("hello") ) ; /* **測試button控件 */ QPushButton *button ; nanVLayout->addWidget( button = new QPushButton("???") ) ; button->setStyleSheet("QPushButton {font:bold 16px; color:red;padding:5px}") ; nanWidget->connect( button , &QPushButton::clicked , [](bool flag){ qDebug()<< "button" ; }) ; /* **測試radiobutton控件 */ QRadioButton *radioButton ; nanVLayout->addWidget( radioButton = new QRadioButton("qradiobutton") ) ; radioButton->setStyleSheet("QRadioButton {font:bold 16px;color:blue;padding:5px}") ; radioButton->connect( radioButton , &QRadioButton::clicked , [](bool flag){ qDebug()<< flag ; }) ; /* **測試ckeckbox控件 */ QCheckBox *check ; nanVLayout->addWidget( check = new QCheckBox("chekcbox") ) ; /* **測試combobox控件 */ QComboBox *combobox ; nanVLayout->addWidget( combobox = new QComboBox() ) ; combobox->addItem("select item1") ; combobox->addItem("select item2") ; combobox->addItem("... ...") ; combobox->setEditable(true) ; lineEdit->setText("start") ; combobox->connect( combobox , SIGNAL(activated(const QString &)) , lineEdit , SLOT(setText(const QString &)) ) ;//這裏的下標要跟着顯示 combobox->setCompleter( new QCompleter(combobox->model()) ) ; /* **測試textedit */ QTextEdit *textEdit ; nanVLayout->addWidget( textEdit = new QTextEdit("textedit") ) ; textEdit->setText("<table border=2> " "<tr><td>good</td><td>good</td><td>good</td></tr>" "<tr><td>nice</td><td>nice</td><td>nice</td></tr>" "</table>" "<img src=./me.png></img>") ; textEdit->connect( textEdit , &QTextEdit::textChanged,[&](){ //問題:textEdit->toPlainText() ;怎麼才能獲取到textEdit的實體,this->sender() //QTextEdit _edit = (QTextEdit *)QObject::sender(); qDebug()<<textEdit->toPlainText() ; }) ; textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded) ; textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn) ; /* **測試groupbox控件 */ QGroupBox *groupBox ; nanVLayout->addWidget( groupBox = new QGroupBox("groupbox") ) ; QHBoxLayout *hLayout ; groupBox->setLayout( hLayout = new QHBoxLayout() ) ; hLayout->addWidget( new QRadioButton("boy") ) ; hLayout->addWidget( new QRadioButton("girl") ) ; /* **測試模擬顯示條slider */ QSlider *slider ; nanVLayout->addWidget( slider = new QSlider() ) ; slider->setMinimum( -100 ) ; slider->setMaximum( 100 ) ; /* **測試數字顯示條 */ QSpinBox *spinBox ; nanVLayout->addWidget( spinBox = new QSpinBox() ) ; spinBox->setMinimum( -100 ) ; spinBox->setMaximum( 100 ) ; slider->connect( slider , SIGNAL(valueChanged(int)) , spinBox , SLOT(setValue(int)) ) ; spinBox->connect( spinBox , SIGNAL(valueChanged(int)) , slider , SLOT(setValue(int)) ) ; /* **測試時間設置 */ nanVLayout->addWidget( new QDateTimeEdit() ) ; /* **測試顯示時間,只讀 */ QLCDNumber *number ; nanVLayout->addWidget( number = new QLCDNumber() ) ; number->display("1314") ; number->setMode(QLCDNumber::Hex) ; number->setSegmentStyle(QLCDNumber::Filled) ; nanWidget->setLayout( nanVLayout ) ; nanWidget->setWindowTitle("control") ; nanWidget->show() ; } void test4GridAndHBox() { QWidget *nanWidget = new QWidget() ; QGridLayout *nanGridLayout = new QGridLayout() ; QHBoxLayout *nanHBoxLayout = new QHBoxLayout() ; nanGridLayout->addWidget( new QLabel("Username") , 1 , 1 ) ; nanGridLayout->addWidget( new QLineEdit() , 1 , 2 ) ; nanGridLayout->addWidget( new QLabel("Username") , 2 , 1 ) ; nanGridLayout->addWidget( new QLineEdit() , 2 , 2 ) ; nanGridLayout->addLayout( nanHBoxLayout , 3 , 2 ) ; nanHBoxLayout->addStretch(1) ; nanHBoxLayout->addWidget( new QPushButton("Login") , 1 ) ; nanWidget->setLayout( nanGridLayout ) ; nanWidget->show() ; } void test3GridLayout() { QWidget *nanWidget = new QWidget() ; QGridLayout *nanLayout = new QGridLayout() ; QPushButton *nanButton = new QPushButton() ; QLineEdit *nanLineEdit = new QLineEdit() ; nanLayout->addWidget( nanLineEdit , 1 , 1 , 1 , 2 ) ; nanLayout->addWidget( new QPushButton , 1, 3 ) ; nanLayout->addWidget( new QLineEdit , 2, 1 , 2 , 1 ) ; nanLayout->addWidget( new QPushButton , 2, 2 ) ; nanLayout->addWidget( new QPushButton , 2, 3 ) ; nanLayout->addWidget( nanButton , 3 , 3 ) ; nanLayout->setColumnStretch( 1 , 1 ) ; nanLayout->setColumnStretch( 2 , 1 ) ;/*設置每列的比重*/ nanLayout->setColumnStretch( 3 , 1 ) ; nanWidget->setLayout( nanLayout ) ; nanWidget->show() ; } void test2HBoxLayout() { QWidget *nanQWidget = new QWidget() ; QLineEdit *nanQLineEdit = new QLineEdit() ; QHBoxLayout *nanHLayout = new QHBoxLayout() ; nanHLayout->addWidget( nanQLineEdit , 1 ) ; //添加,兩個控件之間的距離addspaceing,兩個控件在layout中的比重addstretch nanQWidget->setLayout( nanHLayout ) ; nanQWidget->show() ; } void test1(){ QWidget *w = new QWidget ; QVBoxLayout *vLayout = new QVBoxLayout( ) ; QPushButton *nanButton ; QLineEdit *nanLineEdit ; QLabel *nanLabel ; QString content("null") ; QCompleter nanQCompleter( QStringList()<<"nich"<<"chen"<<"good") ; vLayout->addWidget( nanLineEdit = new QLineEdit() ) ; vLayout->addWidget( nanButton = new QPushButton("right") ) ; nanQCompleter.setFilterMode( Qt::MatchFlag::MatchContains ) ; nanLineEdit->setCompleter( &nanQCompleter ) ; nanLineEdit->setPlaceholderText( "Please input your name" ) ; vLayout->addWidget( nanLabel = new QLabel() ) ; nanLabel->setText( content ) ; w->connect( nanButton , &QPushButton::clicked , [&](){ nanLabel->setText( nanLineEdit->text() ) ; } ) ; w->setLayout( vLayout) ; w->show() ; }
1)、dialog有exec函數,若是是dialog窗口,後邊的窗口時不可選的;
2)、widget和dialog都有show函數,若是經過這個函數顯示這兩種類型的窗口,則兩個窗口都是可選的;
3)、widget主要是在上面放置佈局和控件;
4)、mainwindow能夠顯示菜單,工具欄,狀態欄、托盤等功能。app
這個dialog窗口只是爲了給人們提供更好的可視化操做,可是對於程序員而言,這個操做並非馬上執行的;而是當在窗口選擇關閉後,纔將選擇的結果返回給後臺,後臺才能夠根據選擇的結果進行相應的操做。ide
#include "mydialog.h" mydialog::mydialog(QDialog *parent):QDialog(parent) { QPushButton *button = new QPushButton( "button" , this ) ; connect( button , SIGNAL(clicked()) , this , SLOT(slotButtonClick()) ) ; } void mydialog::slotButtonClick() { #if 0 /*dialog和widget的區別,exec和show的區別而已*/ QDialog *dlg = new QDialog ; QPushButton *button = new QPushButton("close" , dlg ) ; connect( button , SIGNAL(clicked()) , dlg , SLOT(reject()) ) ; int ret = dlg->exec( ) ; //經過exec顯示出來的窗口稱爲,模塊對話框 // 在模塊對話框中,exec有本身的消息循環,而且把app的消息循環接管了 // 若是Dialog是經過exec來顯示,那麼能夠經過accept或者reject來關閉窗口 // 若是Dialog是經過show來顯示,那麼能夠經過close來關閉窗口,這個和QWidget同樣的 // 有許多特殊的dailog:文件選擇,MessageBox,顏色選擇,字體選擇,打印預覽,打印 if( ret == QDialog::Accepted ) qDebug()<<"accepted" ; else if( ret== QDialog::Rejected ) qDebug()<<"rejected" ; #endif #if 0 /*文件選擇:這個窗口能夠選擇保存文件的名稱,而後將路徑+名稱返回,咱們就能夠根據返回路徑名來保存文件。*/ QString strFilename = QFileDialog::getSaveFileName(NULL, "Select file for save", _strDir, "pic file (*.png *.jpg)"); #endif #if 0 /*文件選擇:選擇要打開的文件名(絕對路勁);咱們就能夠根據這個文件路徑來打開相應的文件*/ QString strFilename = QFileDialog::getOpenFileName(NULL, "Select file for open", _strDir, "pic file (*.png *.jpg)"); #endif #if 0 QString strFilename = QFileDialog::getExistingDirectory(); if(strFilename.isEmpty()) { qDebug() << "select none"; return; } qDebug() << strFilename; QFileInfo fileInfo(strFilename); _strDir = fileInfo.filePath(); qDebug() << _strDir; #endif //do something for io ... ... //上面的選擇將絕對路徑名都給拿下來了,若是要進行保存,不是很容易嗎! QPixmap pixmap(this->size()) ; QPainter painter(&pixmap) ; this->render( &painter ) ; pixmap.save(strFilename) ; #if 0 /*顏色選擇對話框:能夠選擇相應的顏色;而後將選擇的顏色返回,這樣咱們就能夠操做了*/ QColorDialog colorDia ; colorDia.exec() ; QColor c = colorDia.selectedColor() ; #endif #if 0 /*字體選擇對話框:能夠選擇字體;而後將選擇的字體信息返回,咱們一樣能夠用這些信息來設置相應的值*/ QFontDialog fontDia ; fontDia.exec() ; QFont font = fontDia.selectedFont() ; #endif #if 0 /*這個也是彈窗對話框,不過只是簡單的選擇如下枚舉中的值,能夠嘗試下效果*/ int ret = QMessageBox::question(this, "????", "realy do .......", QMessageBox::Yes| QMessageBox::No| QMessageBox::YesAll| QMessageBox::NoAll); if(ret == QMessageBox::Yes) { qDebug() << "user select yes"; } if(ret == QMessageBox::No) { qDebug() << "user select no"; } #endif } void mydialog::paintEvent( QPaintEvent *ev ) { } mydialog::~mydialog(void) { }
前面已經介紹過不少繼承這個窗口的控件了,這裏就再也不累述。函數
這個也是給人們提供更好的可視化操做;
一個正常window軟件呈現給客戶的可視化界面;
包括:menu菜單、tool工具欄、status狀態欄、電腦顯示屏右下腳的托盤等。工具
#include "MyWindow.h" MyWindow::MyWindow(QMainWindow *parent):QMainWindow( parent ) { /*menuBar菜單欄,菜單menu*/ QMenuBar *menuBar = this->menuBar() ; _menu = menuBar->addMenu( "&File" ) ; QMenu *edit = menuBar->addMenu( "&Edit" ) ; /*menu菜單中的選項action*/ QAction *openAction = _menu->addAction( "&Open" , this , SLOT(slotOpen()) , QKeySequence::Open ) ; QAction *saveAction = _menu->addAction( "&Save" , this , SLOT(slotOpen()) , QKeySequence::Save ) ; _menu->addSeparator() ;//添加分界線 QAction *closeAction = _menu->addAction( "&Exit" , this , SLOT(close()) , QKeySequence::Close ) ; /*toolBar工具欄*/ QToolBar *toolBar = this->addToolBar( "mimi" ) ; toolBar->addAction( openAction ) ; toolBar->addAction( saveAction ) ; toolBar->addAction( closeAction ) ; /*statusBar狀態欄*/ QStatusBar *statusBar = this->statusBar() ; QLabel *label ; statusBar->addWidget( label = new QLabel("Ok") ) ; label->setText( "<font color=blue>XXXXX... ...</font>" ) ; /*上面的三種欄介紹完以後,剩下的窗口區域就是CentralWidget **若是將widget直接add到mainwindow這個窗口的話, **toolbar是會跟添加進來的widget重疊的*/ MyView *view = new MyView ; this->setCentralWidget( view ) ; /*最後就是window系統右下腳的托盤:system tray icon*/ QSystemTrayIcon *icon = new QSystemTrayIcon ; icon->setIcon( QIcon("./1.png") ) ;//圖標 icon->setToolTip( "luck dog" ) ;//鼠標滑過提示文字 icon->show() ;//展現在右下角 icon->setContextMenu( _menu ) ;//右擊出現的菜單 this->connect( icon , SIGNAL( slotActivated(QSystemTrayIcon::ActivationReason) ) , this , SLOT(slotActivated(QSystemTrayIcon::QSystemTrayIcon::ActivationReason)) ) ; this->installEventFilter(this); } void MyWindow::slotActivated(QSystemTrayIcon::ActivationReason reason){ /*這個沒成功*/ if( reason==QSystemTrayIcon::Trigger ) { if( this->isHidden() ) this->show() ; else this->hide() ; } } bool MyWindow::eventFilter(QObject *o, QEvent *e) { /*實現什麼功能呢?*/ if(o == (QObject*)this && e->type() == QEvent::Close) { return true; } return QMainWindow::eventFilter(o, e); } void MyWindow::slotOpen() { QString fileName = QFileDialog::getOpenFileName() ; qDebug()<<fileName ; /*將打開的文件中的內容顯示在窗口上... ...*/ } bool MyWindow::event(QEvent *ev) { qDebug() << ev; if(ev->type() == QEvent::Close) { return false; } /*怎麼弄才能實現窗口關閉,托盤還在?*/ return QMainWindow::event(ev); } void MyWindow::paintEvent( QPaintEvent * ) { QPainter painter(this) ; painter.drawPixmap( QPoint(0,0) , QPixmap("./1.png") ) ; } void MyWindow::mousePressEvent( QMouseEvent *ev ) { if( ev->button() == Qt::RightButton ) { _menu->exec( QCursor::pos() ) ; } } MyWindow::~MyWindow(void) { }
qDebug()<<"error"<<endl;
QString *str ; QDebug q( str = new QString("object") ) ; q<<"nice"; qDebug()<< *str ;