QT -- signal and slot standard dialog box

QT – signal and slot (3) standard dialog box

Create a new project Qt Widgets, select qwidth as the class, and the class name is MyWidget. Post completion ui design

1. Color dialog box QColorDialog

Now add #include < qdebug > and #include < qcolordialog > header files in mywidget.cpp, then enter the clicked() of the "color dialog" button from the design mode, click the signal slot, and change:

void MyWidget::on_pushButton_clicked()
{
    QColor color=QColorDialog::getColor(Qt::red,this,tr("Color dialog "));
    qDebug()<<"color:"<<color;
}

If you can't, press F1 to check the help

Borrow QColorDialog::getColor method
Its three parameters are: setting the initial color, specifying the parent window, and setting the dialog box title
Qt::red here is Qt's built-in color object, ennum Qt::GlobalColor

getColor returns a QColor type data.
qDebug output color information

Select the color, click OK, and qDebug outputs the color information

color: QColor(ARGB 1, 1, 0, 0)

The four values here represent transparency (alpha) and red respectively ®, Green (g) and blue (blue)

The value is 0.0 ~ ~ 1.0, or 0 ~ 255
There is also a simple conversion between the two
0 corresponds to 0.0255 corresponds to 1.0, with an accuracy of 1 / 255

In the color dialog box, you can also add the setting of alpha, that is, use the last parameter in the getColor() function:

 QColor color=QColorDialog::getColor(Qt::red,this,tr("Color dialog "),QColorDialog::ShowAlphaChannel);

QColorDialog::ShowAlphaChannel

Used to display alpha settings. You can run the program

More alpha settings

The above is to use the static function of QColorDialog to display the color dialog box. If you want to create objects and more flexible settings, you can create objects first

void MyWidget::on_pushButton_clicked()
{
    /*Static method*/
//QColor color=QColorDialog::getColor(Qt::red,this,tr("color dialog"), QColorDialog::ShowAlphaChannel);

    /*create object*/
QColorDialog dialog(Qt::red,this); //create object
dialog.setOption(QColorDialog::ShowAlphaChannel);  //Show alpha options
dialog.exec();                 //Run dialog in modal mode
QColor color =dialog.currentColor(); //Get current color

 qDebug()<<"color:"<<color;
}

2 file dialog box QFileDialog

File dialog
Add header file #include < QFileDialog >
Then go to the slot in the file dialog box from the ui interface

//Slots in the file dialog box
void MyWidget::on_pushButton_2_clicked()
{
    QString fileName=QFileDialog::getOpenFileName(this,tr("File dialog"),"D:",tr("Picture file(*png *jpg)"));
  
}

The getOpenFileName function of QFileDialog is used here
You can crtl Click to go to that function,
You can f1 help. Anyway, these two can help you
subsequently
getOpenFileName this function will run a file dialog box in modal mode. After opening, it will return the selected file name

 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                  "/home",
                                                  tr("Images (*.png *.xpm *.jpg)"));

Its four parameters are used to specify the parent window, set the dialog box title, specify the default open directory path, and set the file type filter

getOpenFileName(this,tr("File dialog"),"D:",tr("Picture file(*png *jpg)"));

If no filter is set, all types of files are selected by default

Only png and jpg file formats are selected here

In addition, you can also set multiple different types of filters separated by
For example: add txt type

getOpenFileName(this,tr("File dialog"),"D:",tr("Picture file(*png *jpg);;text file(*txt)"));

Then add the qDebug() output

//Slots in the file dialog box
void MyWidget::on_pushButton_2_clicked()
{
    QString fileName=QFileDialog::getOpenFileName
    (this,tr("File dialog"),"D:",tr("Picture file(*png *jpg);;text file(*txt)"));
    qDebug()<< "fileName:" << fileName;
}


Select a photo of disk d

At this time, add txt to select TXT file


The previous program intelligently selects a file. If you want to select multiple files
Just like the English plural, just add an s and use the function getOpenFileNames

//Slots in the file dialog box
void MyWidget::on_pushButton_2_clicked()
{
    QStringList fileNames=QFileDialog::getOpenFileNames
    (this,tr("File dialog"),"D:",tr("Picture file(*png *jpg);;text file(*txt)"));
    qDebug()<< "fileNames:" << fileNames;
}

Multiple file names are stored in QStringList type variables.
QStringList official: https://doc.qt.io/qt-5/qstringlist.html

Of course, instead of using these static functions, you can create dialog objects to operate.

//Slots in the file dialog box
void MyWidget::on_pushButton_2_clicked()
{
//    QStringList fileNames=QFileDialog::getOpenFileNames(this,tr("file dialog"), "D:",tr("picture file (* png *jpg);; text file (* txt)");
    QFileDialog dialog1(this);


    //qDebug()<< "fileNames:" << fileNames;
    QStringList fileNames= dialog1.getOpenFileNames
    (this,tr("File dialog"),"D:",tr("Picture file(*png *jpg);;text file(*txt)"));
    qDebug()<< "fileNames:" << fileNames;
}

There are other functions besides getOpenFilename
The QFileDialog class also provides the getSaveFilename() function to implement the save file dialog box and the save file as dialog box

QString fileName =  QFileDialog :: getSaveFileName( this , tr( "Save File" ) , 
                           "/home/jana/untitled.png" , 
                           tr( "Images (*.png *.xpm *.jpg)" ));

dialog1.getSaveFileName(this,tr("file save"), "C:/QtCode",tr("picture file (* png *jpg)");
Like this
You can also getExistingDirectory(); / / get the file path

For similar usage, see help, etc., which is similar to the above

3. Font dialog box QFontDialog

First add #include < qfontdialog > header file, and then go to the slot of the font dialog button to its slot function

void MyWidget::on_pushButton_3_clicked()
{
    // OK is used to mark whether the "OK" button is pressed
       bool ok;//bool type, 0 or 1

       QFont font = QFontDialog::getFont(&ok, this);
       // If you press the OK button, let the font dialog button use the new font
       // If the "Cancel" button is pressed, the information is output
       if (ok) ui->pushButton_3->setFont(font);
       else qDebug() << tr("No font selected!");
}


4. Input dialog QInputDialog

The input dialog box QInputDialog class is used to provide a dialog box that allows users to enter a single value or string.

First add the header file #include < qinputdialog >, and then go to the slot

void MyWidget::on_pushButton_4_clicked()
{
    bool ok;
       // Get string
       //QLineEdit::Normal is the initial default value tr - > admin
       QString string = QInputDialog::getText
   (this, tr("Input string dialog box"), tr("Please enter user name:"), 
   QLineEdit::Normal,tr("admin"), &ok);
       if(ok) qDebug() << "string:" << string;
       // Get integer
       //The values in brackets are: default value, minimum value, maximum value, and 10 addition and subtraction changes each time
       int value1 = QInputDialog::getInt(this, tr("Enter integer dialog box"),
                                         tr("Please enter-1000 Values between and 1000"),
                                          100, -1000, 1000, 10, &ok);
       if(ok) qDebug() << "value1:" << value1;
       // Get floating point number
       //The values in brackets are: default value, minimum value, maximum value, and 2 indicates that the number of decimal places is 2
       double value2 = QInputDialog::getDouble
   (this, tr("Import floating point number dialog box"),tr("Please enter-1000 Values between and 1000"),
    0.00, -1000, 1000, 2, &ok);
       if(ok) qDebug() << "value2:" << value2;

       QStringList items;
       items << tr("Entry 1") << tr("Entry 2");
       // Get entry
       //Entries are already provided above, 12
       //0 means the first entry by default. The parameter true sets whether the entry can be changed. True means it can be changed.
       QString item = QInputDialog::getItem
    (this, tr("Input entry dialog box"),tr("Please select or enter an entry"), items, 0, true, &ok);
       if(ok) qDebug() << "item:" << item;


        //Static functions are used here. You can also replace the above functions with objects
}




The explanations are in the notes. Press f1 to see the help

5. Message dialog box QMessageBox

The message dialog box QMessageBox class provides a modal dialog box to notify the user of some information, or ask the user a question and get the answer.
First add the header file #include < qmessagebox >, and then go to the slot

void MyWidget::on_pushButton_5_clicked()
{
    // Question dialog
        int ret1 = QMessageBox::question(this, tr("Question dialog"),
        tr("You know Qt Are you?"), QMessageBox::Yes, QMessageBox::No);
        if(ret1 == QMessageBox::Yes) qDebug() << tr("Question!");
        // Prompt dialog box
        int ret2 = QMessageBox::information(this, tr("Prompt dialog box"),
      tr("This is Qt Books!"), QMessageBox::Ok);
        if(ret2 == QMessageBox::Ok) qDebug() << tr("Tips!");
        // Warning dialog box
        int ret3 = QMessageBox::warning(this, tr("Warning dialog box"),
       tr("Can't end early!"), QMessageBox::Abort);
        if(ret3 == QMessageBox::Abort) qDebug() << tr("Warning!");
        // Error dialog
        int ret4 = QMessageBox::critical(this, tr("Critical error dialog box"),
  tr("A serious error was found! Close all files now!"), QMessageBox::YesAll);
        if(ret4 == QMessageBox::YesAll) qDebug() << tr("error");
        // About dialog box
        QMessageBox::about(this, tr("About dialog box"),
                           tr("yafeilinux betake Qt and Qt Creator Our popularization work!"));
}




Four different types of message dialog boxes are created here, with different icons and prompt tones (set by the system),
Several parameters are used to set the parent window, title bar, display information and buttons.

The buttons used here are standard buttons provided by the QMessageBox class.

The return values of these static functions are those standard buttons, which are specified by the qmessagebox:: standardbutton enumeration type. You can use the return value to judge which button the user pressed.

About the dialog box - > the about() function does not return a value because it has only one button by default. Similarly, there is an aboutQt() function to display the current Qt version and other information.

If you want to use custom icons and buttons, you can create a QMessageBox class object and use related functions to operate.

6. Progress dialog box qpprogressdialog

First add #include < qpprogressdialog > header file, and then go to the slot

void MyWidget::on_pushButton_6_clicked()
{
    //(display text of dialog box, display text of cancel button, minimum value, maximum value, parent window)
    QProgressDialog dialog(tr("File copy progress"), tr("cancel"), 0, 50000, this);
        dialog.setWindowTitle(tr("Progress dialog box"));     // Set window title
        dialog.setWindowModality(Qt::WindowModal);  // Set the dialog box to modal prohibit parent window
        dialog.show();
        for(int i=0; i<=50000; i++) {                // Demonstrate replication progress
            dialog.setValue(i);                     // Sets the current value of the progress bar
            QCoreApplication::processEvents();      // Avoid interface freezing
            if(dialog.wasCanceled()) break;         // Press the Cancel button to interrupt
        }
        qDebug() << tr("Copy finished!");
}

The method used here is to create an object. A qpprogressdialog class object dialog is created.

7. Error message dialog box QErrorMessage

Dialog box displaying error messages
First, open the mywidget.h file and add the pre declaration:
class QErrorMessage

Then add the private object QErrorMessage *errordlg;

Next, add the header file #include < qerrormessage > to mywidget.cpp and add it in the constructor:
errordlg = new QErrorMessage(this);
Then go to the slot

void MyWidget::on_pushButton_7_clicked()
{
   errordlg->setWindowTitle(tr("Error message dialog box"));
   errordlg->showMessage(tr("Here is the error message!"));
}


First, create a QErrorMessage dialog box, and call its showMessage() function to display the error message. When calling this function, the dialog box will be displayed in a non modal form

It has a show this message again check box by default. You can choose whether to display the same error in the future

8. QWizard dialog box

It provides a framework for designing wizard interfaces, such as wizards when installing software.

QWizard is called a framework because it has all the functions of designing a wizard. You can use it to achieve the desired effect.

Qt includes three sample programs: Trivial Wizard, License Wizard and Class Wizard;

Open the mywidget.h file, add the header file #include < qwizard >, and add the private type function declaration in the declaration of MyWidget class:

 QWizardPage *createPage1();//New addition
    QWizardPage *createPage2();//New addition
    QWizardPage *createPage3();//New addition


The three pointer functions here are QWizardPage class objects, which are used to generate three wizard pages.
Then define these three functions in mywidget.cpp file:

/*wizard dialogs */
QWizardPage * MyWidget::createPage1()  // Wizard page 1
{
    QWizardPage *page = new QWizardPage;
    page->setTitle(tr("introduce"));
    return page;
}

QWizardPage * MyWidget::createPage2()  // Wizard page 2
{
    QWizardPage *page = new QWizardPage;
    page->setTitle(tr("User selection information"));
    return page;
}

QWizardPage * MyWidget::createPage3()  // Wizard page 3
{
    QWizardPage *page = new QWizardPage;
    page->setTitle(tr("end"));
    return page;
}

Wizard pages are created in each function and their titles are set. Next, go to the slot

void MyWidget::on_pushButton_8_clicked()
{
       QWizard wizard(this);
       wizard.setWindowTitle(tr("wizard dialogs "));
       wizard.addPage(createPage1());     // Add wizard page
       wizard.addPage(createPage2());
       wizard.addPage(createPage3());
       wizard.exec();
}

First, a new QWizard object is created, and then three pages are added to it using the addPage() function
Three wizard generation page functions are called directly


Tags: Qt UI

Posted on Sun, 05 Dec 2021 06:53:08 -0500 by AcidRain