dialog box-qdialog

Source: Internet
Author: User

dialog box provides a convenient way for users to interact with the application, such as system options, font settings, etc.

Modal dialog box: The modal dialog box does not disappear before the user can interact with other windows of the same application, knowing that the dialog box is closed.

Customize dialog box

Logindlg.h

#ifndef Logindlg_h
#define Logindlg_h
#include <QDialog>
The purpose of this code is to tell the compiler that the Qlineedit class already exists
Class Qlineedit;   Only pointers to Qlineedit objects are used in the header file of Class Clogindlg, so the GCC compiler does not need to know the definition of the Qlineedit class when compiling the header file
Class Clogindlg:public Qdialog   //Custom class inheritance Qdialog
{
    Q_object            //The function of this macro is to start some features of the QT meta-object (such as supporting signals and slots), which must be placed in the private area of the class definition
Public
    Clogindlg (qwidget* =0);  is a constructor that specifies a default value of NULL for a parameter that points to Qwidget
    The parameter defines the parent widget for the custom dialog box object, and the default value of NULL means that the custom dialog box does not have a parent widget
Public Slots:
    virtual void accept ();
Private
    Qlineedit *usrlineedit;
    Qlineedit *pwdlineedit;
};
#endif//Logindlg_h

Logindlg.cpp
#include <QtGui/QtGui>
#include "Logindlg.h"
Clogindlg::clogindlg (Qwidget *parent)
    : Qdialog (parent)
{
    Qlabel *usrlabel=new Qlabel (tr ("User name:"));
    Qlabel *pwdlabel=new Qlabel (tr ("Password:"));
    Usrlineedit=new Qlineedit;
    Pwdlineedit=new Qlineedit;
    Pwdlineedit->setechomode (qlineedit::P assword);
    Qgridlayout *gridlayout=new qgridlayout;
    Gridlayout->addwidget (usrlabel,0,0,1,1);
    Gridlayout->addwidget (usrlineedit,0,1,1,3);
    Gridlayout->addwidget (pwdlabel,1,0,1,1);
    Gridlayout->addwidget (pwdlineedit,1,1,1,3);
    Qpushbutton *okbtn=new Qpushbutton (tr ("OK"));
    Qpushbutton *cancelbtn=new Qpushbutton (tr ("Cancel"));
    Qhboxlayout *btnlayout=new qhboxlayout;
    Btnlayout->setspacing (60);
    Btnlayout->addwidget (OKBTN);
    Btnlayout->addwidget (CANCELBTN);
    Qvboxlayout *dlglayout=new qvboxlayout;
    Dlglayout->setmargin (+);             Set the internal subassembly distance from the layout manager boundary to 40
    Dlglayout->addlayout (gridLayout);
    Dlglayout->addstretch (+);    Adding a size of 40 stretch,stretch between vertical layout managers can be scaled up and down
    Dlglayout->addlayout (btnlayout);
    SetLayout (dlglayout);
    Connect (okbtn,signal (clicked ()), This,slot (Accept ()));
    Connect (cancelbtn,signal (clicked ()), This,slot (Reject ())); Reject () hides the login dialog and sets the return code of the dialog box to qdialog::rejected, where the dialog closes and the slot function qdialog::exec () That launches the dialog box returns qdialog::rejected.
    Setwindowtitle (TR ("Landing"));
    Resize (300,200);  The size of the Reset dialog box is (300,200), which is 300 and the width is 200.
}
void Clogindlg::accept ()
{
    if (Usrlineedit->text (). Trimmed () ==tr ("admin")
    &&pwdlineedit->text (). Trimmed () ==tr ("admin"))
    {
        Qdialog::accept ();  If the user enters all correctly, calls the qdialog::accept () slot function in the parent class, the function closes the modal dialog box, the setting dialog runs as qdialog::accepted, and sends the qdialog::finished (int Result) signal.
    }
    Else
    {
        Create and display a modal warning window
        Qmessagebox::warning (This,
                             TR ("Warning"),
                             TR ("User name or password is wrong!"),
                             Qmessagebox::yes);
        Usrlineedit->setfocus ();   Position the cursor to an edit box object
    }
}
Main.cpp
#include <QtGui>
#include "Logindlg.h"
int main (int argc,char **argv)
{
    Qapplication A (ARGC,ARGV);
    QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("Utf-8"));
    Qtranslator Translator;   The Qtranslator class provides support for internationalization of text output
    {        //braces are used to destroy the stack objects that are no longer in use in time
        Qstringlist environment=qprocess::systemenvironment ();   Get environment variables for a process
        QString str;
        BOOL Bfinded=false;    Defines an identifier for a lookup result
        foreach (str,environment)  //Find a string that begins with "qtdir=" in the list of obtained environment variables
        {
            if (Str.startswith ("qtdir="))
            {
                Bfinded=true;
                Break
            }
        }
        if (bfinded)   //The function of this segment of code is to install the translator for the application
        {
            Str=str.mid (6);  Use the mid () function to get the value of the environment variable, that is, the installation path of QT
            The following statement is equivalent to calling the Qtranslator:;load ("Qt_zh_cn", "$QTDIR/translations/");
            Bfinded=translator.load ("Qt_" +qlocale::system (). Name (),   //is a translation file that loads QT
                                    Str.append ("/translations/"));
            if (bfinded)
                Qapp->installtranslator (&translator);
            Else
                Qdebug () <<qobject::tr ("No Support Center for QT internationalization translation files!");
        }
        Else
        {
            Qdebug () <<qobject::tr ("must set QTDIR environment variable!");
            Exit (1);
        }
    }
    Clogindlg Dlg;
    return Dlg.exec ();
}

Examples of advanced applications of Qmessagebox

Qmessagebox box;

Box.setwindowtitle (tr ("warning");

Box.seticon (qmessagebox::warning);

Box.settext (TR ("Program installation error, exit?");

Box.setstandardbuttons (Qmessagebox::yes

| Qmessagebox::no);

Box.setdetailedtext (TR ("see if the installation media is damaged.) ");

Switch (box.exec ())

{

Case Qmessagebox::yes:

Proceed to the next

Break

Case Qmessagebox::no:

Proceed to the next

Break

Default

Make default processing

Break

}

Qt built-in dialog box





dialog box-qdialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.