Home>

Recently read qt tutorial and software to learn qt. The programming of this book is carried out on qt creator.I prefer to develop on vs2012,So based on your own experience in the experiment,Take note of some problems and solutions for qt programming in vs2012,I use qt5.3 version.

First, create a new project

Select qt application and fill in the project name

Always next, base class select qwidget, click finish to create

At this time, the project does not add a corresponding include file, and there are many places that display "can not open source file ...". Select the menu bar project->user properties ... to enter the header setting page

Click "additional include directories" to edit,Add the following folders under your qt installation directory

Create a new conversation class,Right-click the project name,Add new class

Select qt5 classes, double-click qt5guiclasses

Fill the base class with qdialog and click finish to create it

Then ui layout is performed, and the following interface is designed in logindlg.ui:the line input box is line edit. The input box after the user name sets its object name to usrlineedit in properties, the input box after the password is pwdlineedit, the login button is loginbtn, and the exit button is exitbtn. After setting, as shown below:

Set the signal and slot of the login button as above.The slot function must be written by itself,So slot select there and click edit,A new slot1 () was renamed on_loginbtn_clicked (). Click OK.Save the qt designer and close.

Back to vs2012, modify main.cpp:

#include "user.h"
#include<qtwidgets/qapplication>
#include "logindlg.h"
int main (int argc, char * argv [])
{
  qapplication a (argc, argv);
  user w;
  logindlg login;
  if (login.exec () == qdialog ::accepted)
  {
  w.show ();
  return a.exec ();
  }
  else return 0;
}

Then modify logindlg.cpp, first add the previous slot function declaration to logindlg.h.

#ifndef logindlg_h
#define logindlg_h
#include<qdialog>
#include "ui_logindlg.h"
class logindlg:public qdialog
{
  q_object
public:
  logindlg (qwidget * parent=0);
  ~ logindlg ();
private:
  ui ::logindlg * ui;//Here ui is defined as a pointer to the class
private slots:
  void on_loginbtn_clicked ();//Slot function declaration
};
#endif //logindlg_h

Finally define the slot function in logindlg.cpp,Enter the account password correctly to enter the main window,Otherwise, an error is reported.

void logindlg ::on_loginbtn_clicked ()
{
  if (ui->usrlineedit->text () == tr ("caddress")&&ui->pwdlineedit->text () == tr ("123456"))
    accept ();
  else {
    qmessagebox ::warning (this, tr ("warning"), tr ("user name or password error!"), qmessagebox ::yes):
  }
}

At this point, the program has many errors.Most header files were not found,Continue to add the corresponding folders as above:

After adding, click compile,The corresponding header file is found.

logindlg.cpp complete code:

#include "logindlg.h"
#include<qmessagebox>//Add this header file to use qmessagebox
logindlg ::logindlg (qwidget * parent)
 :qdialog (parent)
{
 (* ui) .setupui (this);//Here ui needs to be a class type,So we need to solve the pointer
}
logindlg ::~ logindlg ()
{
}
void logindlg ::on_loginbtn_clicked ()
{
 if (ui-&us;usrlineedit->text () == tr ("caddress")&&ui->pwdlineedit->text () == tr ("123456")) //Need a pointer here,Use ui directly
  accept ();
 else {
  qmessagebox ::warning (this, tr ("warning"), tr ("user name or password error!"), qmessagebox ::yes);
 }
}

Click compile to achieve the function

  • Previous Examples of text preprocessing and feature extraction using python
  • Next Python find file name contains specified string method