QT 인터페이스 프로그래밍 지식

9916 단어
좋은 자료

0. QT 패키지 고려 사항

  • 만약에 VC 컴파일러를 사용한다면 QT 기능 라이브러리를 가지고 있어야 하는 것 외에 VC 운행 환경 dll도 휴대해야 한다.
  • VC 실행 환경 dll:msvcp140.dll와vcruntime140.dll, 모두 32비트;

  • 1. 신호와 슬롯

  • 직접 정의법(QT4와 QT5 두 가지 정의 방법, 코드 직접 정의와 편집기 정의)
  • /*         :
    1.            ,  Qt     signals   public,private, protected。
    2.           ,       void。
    3.           。
    4.          QOBject ,          Q_OBJECT。
    5.        ,         ,          ,         ,            ,                       ,                      。
    6.        ,         :      ,           ,           。
    7.      emit   。
    8.                 。
    */
    
    /*        :
    1.        C++  ,             。            ,        。
    2.       :public/protected/private slots:
    3.   Qt4   , Qt5            ,              ,       Lambda       。          ,             .
    */
    connect(pushButton, SIGNAL(clicked()), dialog,  SLOT(close()));
    connect(pushButton, QPushButton::clicked, dialog,  QDialog::close);
    
  • lambda 표현식 직접 실행 슬롯 함수 사용 참조
  • #include "widget.h"
    #include 
    #include 
    #include
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {       
        vLayout = new QVBoxLayout(this);
        b1=new QPushButton("     ",this);
        b2=new QPushButton("      ",this);
        b3=new QPushButton("      ",this);
        vLayout->addWidget(b1);
        vLayout->addWidget(b2);
        vLayout->addWidget(b3);
        connect(b1,&QPushButton::clicked,[=](){this->close();});
    }
    
    Widget::~Widget()
    {}
    
  • 대상명 관련 신호조(코드 수동 정의 및 편집기 정의)를 통해
  • //   
    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    #include
    
    class Widget : public QWidget
    {
        Q_OBJECT
        public:
        Widget(QWidget *parent = 0);
        ~Widget();
        private:
        QPushButton *b1;
        private slots:
        void on_button1_released();
    };
    
    #endif // WIDGET_H
    
    //   
    #include "widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        resize(500,300);
        b1=new QPushButton("CLOSE",this);
        b1->move(200,200);
        b1->setObjectName(QStringLiteral("button1"));
        QMetaObject::connectSlotsByName(this);
    }
    
    Widget::~Widget()
    {}
    
    void Widget::on_button1_released()
    {
        this->close();
    }
    
    //  
    b1->setObjectName(QStringLiteral("button1"));       //       
    QMetaObject::connectSlotsByName(this);              //     
    void Widget::on_button1_released();                //        ,     
    
  • QSignalMapper 대량 전송 신호
  • #include "widget.h"
    
    #include 
    #include 
    #include 
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {   
        //      ,          
        QVBoxLayout* vLayout = new QVBoxLayout(this);
    
        //     ,           ,         
        QLineEdit* edit = new QLineEdit;
        edit->setAlignment(Qt::AlignRight);
        vLayout->addWidget(edit);//            
    
        //       
        QSignalMapper* signalMapper = new QSignalMapper(this);
    
        //  0-9   ,          
        QGridLayout *gridLayout = new QGridLayout;
        for (int i = 0; i < 10; ++i)
        {
            QString txt = QString::number(i);
            QPushButton *button = new QPushButton(txt);
            connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
            signalMapper->setMapping(button, txt);//             
            gridLayout->addWidget(button, i / 3, i % 3);//    3 
        }
    
        //            
        connect(signalMapper, SIGNAL(mapped(QString)),
                edit, SLOT(setText(QString)));
    
        vLayout->addLayout(gridLayout);
        resize(200, 200);
    }
    Widget::~Widget()
    {}
    
    //  :    
    connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(button, txt);//             
    connect(signalMapper, SIGNAL(mapped(QString)),edit, SLOT(setText(QString)));
    

    2. 모드 대화 상자 및 비모드 대화 상자

    #include "widget.h"
    #include 
    #include 
    #include
    #include
    #include "customdialog.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {       
        vLayout = new QVBoxLayout(this);
        b1=new QPushButton("     ",this);
        b2=new QPushButton("      ",this);
        b3=new QPushButton("      ",this);
        vLayout->addWidget(b1);
        vLayout->addWidget(b2);
        vLayout->addWidget(b3);
        connect(b1,&QPushButton::clicked,[=](){
            //       
            QDialog* dlg = new QDialog(this);
            dlg->setAttribute(Qt::WA_DeleteOnClose);//        ,    
            dlg->setWindowModality(Qt::WindowModal);//      setModel(true)
            dlg->show();
        });
        connect(b2,&QPushButton::clicked,[=](){
            //        
            QDialog *modellessDig=new QDialog;
            modellessDig->setWindowTitle("      ");
            modellessDig->setAttribute(Qt::WA_DeleteOnClose);
            modellessDig->show();
        });
        connect(b3,&QPushButton::clicked,[=](){
            //        
            CustomDialog *dlg=new CustomDialog(this);
            dlg->setAttribute(Qt::WA_DeleteOnClose);
            int ret = dlg->exec();
            if (ret == QDialog::Accepted)
            {
                qDebug()<

    3. 표준 대화상자

    //     
    int i = QInputDialog::getInt(this, tr("  "),tr("    "), 25, 16, 100, 1, &ok);
    double d = QInputDialog::getDouble(this, tr("  "),tr("      "), 1000, 100, 10000, 1, &ok);
    
    QStringList items;
    items << tr("    ") << tr("Dota") << tr("Dota2") << tr("LOL");
    bool ok;
    QString item = QInputDialog::getItem(this, tr("  "),tr("     "), items, 0, false, &ok);
    
    QString text = QInputDialog::getText(this, tr("  "),tr("    "), QLineEdit::Normal,tr(" "), &ok);
    
    QString text = QInputDialog::getMultiLineText(this, tr("  "),tr("      "), "   ", &ok);
    
    //     
    QColor color = QColorDialog::getColor(Qt::green, this, "    ", QColorDialog::DontUseNativeDialog);
    
    //     
    QFont font =QFontDialog::getFont(&ok,this);
    
    //      :
    QString directory = QFileDialog::getExistingDirectory(this,"Get directory:","./");
    
    //     
    QString fileName = QFileDialog::getOpenFileName(this,"Open a file","./","All Files (*);;Text Files (*.txt)");
    
    //         
    QStringList files = QFileDialog::getOpenFileNames(this,"Open multiple files:","./","All Files (*);;Text Files (*.txt)");
    
    //       
    QString fileName = QFileDialog::getSaveFileName(this,tr("Save file:"),"./",tr("All Files (*);;Text Files (*.txt)"));
    
    //     
    qDebug()<<:critical qdebug="" you="" no="" or="" that="" qmessagebox::no="" qmessagebox::cancel=""/>

    4. 템플릿 클래스 및 도구 클래스

    QString
    QList
    QLinkedList
    QVector
    QMap
    QHash
    QVariable
    
         
    

    5. 창 배치


    (1) QSplitter


    QSplitter 소형 인스턴스
    //  
    QSplitter *splitterMain = new QSplitter(Qt::Horizontal, Q_NULLPTR);
    QTextEdit *textLeft = new QTextEdit(QObject::tr("Left Widget"),splitterMain);
    QTextEdit *textRight = new QTextEdit(QObject::tr("Right Widget"), splitterMain);    
    QTextEdit *textRight2 = new QTextEdit(QObject::tr("Right Widget2"), splitterMain);  
    splitterMain->setStretchFactor(1, 1);
    splitterMain->setWindowTitle(QObject::tr("Splitter"));
    splitterMain->show();
    
    //  QSplitter      QWidget
    //setStretchFactor(int index,int factor)     :                
    //                    
    splitterMain->setStretchFactor(0, 1);
    splitterMain->setStretchFactor(1, 3);
    splitterMain->setStretchFactor(2, 3);
    //factor     ,    0         ,   0
    

    (2) QDockWidget

    setWindowTitle("  ");
    QTextEdit *te = new QTextEdit(this);
    te->setText("Main Window");
    te->setAlignment(Qt::AlignCenter);
    setCentralWidget(te);
    
    QDockWidget *dock1 = new QDockWidget("    1", this);
    dock1->setFeatures(QDockWidget::DockWidgetMovable);
    dock1->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    QTextEdit *te1 = new QTextEdit();
    te1->setText("dock1, which can be moded to left or right side");
    dock1->setWidget(te1);
    addDockWidget(Qt::RightDockWidgetArea, dock1);
    
    QDockWidget *dock2 = new QDockWidget("    2", this);
    dock2->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable);
    QTextEdit *te2 = new QTextEdit();
    te2->setText("dock2, which can be closed and floated");
    dock2->setWidget(te2);
    addDockWidget(Qt::RightDockWidgetArea, dock2);
    
    QDockWidget *dock3 = new QDockWidget("    3", this);
    dock3->setFeatures(QDockWidget::AllDockWidgetFeatures);
    QTextEdit *te3 = new QTextEdit();
    te3->setText("dock3, all features");
    dock3->setWidget(te3);
    addDockWidget(Qt::RightDockWidgetArea, dock3);
    

    (3) QStackedWidget

    label1 = new QLabel("WindowTest1");
    label2 = new QLabel("WindowTest2");
    label3 = new QLabel("WindowTest3");
    widget = new MyWidget();
    stack = new QStackedWidget(this);
    stack->addWidget(label1);
    stack->addWidget(label2);
    stack->addWidget(label3);
    stack->addWidget(widget);
    stack->setCurrentIndex(1);
    

    (4) 3가지 기본 레이아웃

    QHBoxLayout
    QVBoxLayout
    QGridLayout
    

    6. 중국어 난자 문제


    참고 자료
  • QString::fromLocal8Bit("안녕하세요")
  • QStringLiteral("중국")
  • pragma execution_character_set("utf-8")이 제일 좋아요.

  • 좋은 웹페이지 즐겨찾기