QLabel

QLabel is one of the most common controls that you can use to display text, images, animations, and so on.

To use QLabel, you still need to import the header file.

According to the text

Let’s implement the simplest QLabel that displays only text:

As you can see, the string is not fully displayed, and since the coordinates are not specified, the coordinates default to (0,0) relative to the parent element, so the QLabel overwrites the menu bar of the MainWindow, causing the menu bar to be unable to drag.

AdjustSize () P.S: Strings are not shown completely because there is a member function in the QLabel class that sets the default size of QLabel. We can call adjustSize() to adjust the width and height with the content so that the content can be shown completely. Of course, there are other ways to do this, which I won’t go over here.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QLabel *pQLabel=new QLabel(this);
    pQLabel->setText("I am a QLabel!!");
    pQLabel->adjustSize(a);this->setFixedSize(300.400);
    pQLabel->move(100.100);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

Display images

To display images in QLabel, define a QPixmap object and specify its path, then call the setPixmap method of QLabel and pass in the QPixmap object as a parameter.

P.S: Pay attention to the direction of the slash in the path!!

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Display the image
    QLabel *pQLabel=new QLabel(this);
    QPixmap mPixmap;
    mPixmap.load("C:/Users/MSI-NB/Desktop/masami.jpg");
    pQLabel->setPixmap(mPixmap);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

Notice that the image shows only a small portion, and from here we can also see what the default size of the QLabel object is. AdjustSize () is an adjustSize() method for adjusting pictures.

Effect:

It shows a smaller range… That’s because it doesn’t detect text, and of course there’s only this small area. AdjustSize () is only suitable for adjustSize().

How to solve the problem of image adaptive, first dig a pit -_ – | |, we now use as QLabel object first set the size of this kind of method to make the whole picture full display.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   // Display the image
    QLabel *pQLabel=new QLabel(this);
    QPixmap mPixmap;
    mPixmap.load("C:/Users/MSI-NB/Desktop/masami.jpg");    // Get the image path
    pQLabel->setFixedSize(500.333);    // Set the QLabel size
    pQLabel->setScaledContents(true);   // The contents are stretched to fill the container
    this->setFixedSize(pQLabel->size());    // Also size the main window to prevent stretching
    pQLabel->setPixmap(mPixmap);    // Load the QPixmap object into the QLabel object
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

So you can see the whole sesame paste

According to the animation

To display an animation in QLabel, you define a QMovie object, specify a path for it, and then call the setMovie method of QLabel, passing in the QMovie object as a parameter. Of course, remember to introduce the header file first.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
#include <QMovie>
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Display animation
    QLabel *pQLabel=new QLabel(this);
    QMovie *mMovie=new QMovie("C:/Users/MSI-NB/Desktop/masami.gif");
    pQLabel->setFixedSize(224.168);
    pQLabel->setScaledContents(true);   // The contents are stretched to fill the container
    this->setFixedSize(pQLabel->size());
    pQLabel->setMovie(mMovie);
    mMovie->start(a);// Very important! Otherwise nothing will be displayed, right
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

Beauty who are you?

Display hyperlinks

To add a hyperlink to the interface, add a piece of HTML code to the content of the QLabel and call the setOpenExternalLink() method. If you don’t use this method, you can only use signals and slots to achieve the function of clicking the hyperlink to pop up the web page, obviously it is very troublesome to do so.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
#include <QMovie>
#include <QLineEdit>
#include <QString>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QLabel *pQLabel=new QLabel(this);
    // Display hyperlinks
    pQLabel->setText(");
    pQLabel->setOpenExternalLinks(true);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

QLineEdit

A single line text input box is also very common. To implement a single line text input box, you need to introduce a header file.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLineEdit>    // Implement the header file that the single-line text input box must import
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Single line of text
      QLineEdit *pLineEdit=new QLineEdit(this);
      pLineEdit->move(100.100);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

A rudimentary one-line text input box appears.

SetText () and text ()

In a single-line text input box, the most common methods are to set the content and get the content, calling the setText() and text() methods on the QLineEdit object, respectively.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
#include <QMovie>
#include <QLineEdit>
#include <QString>
#include <QDebug>
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Single line of text
      QLineEdit *pLineEdit=new QLineEdit(this);
      pLineEdit->setText("Set single line input box contents");    // Set the content
      QString contents=pLineEdit->text(a);// Get the content
      qDebug()<<contents;
      pLineEdit->move(100.100);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

With these two methods, we have achieved both to set the content of the single line text input box, and achieved the content of the text input box and output.

setEchoMode()

In real life, when we enter a password, the single-line text box will not show us exactly what we typed, but instead will be replaced by “*” or “●”. To do this, you call setEchoMode() to set the mode for the text input box. The value can be:

  1. QLineEdit::Normal display mode, according to the input content.
  2. QLineEdit::NoEcho displays nothing and user input cannot be seen in this mode.
  3. QLineEdit::Password Password mode, the entered characters are converted to special characters according to the platform.
  4. QLineEdit: : PasswordEchoOnEdit edit display character or character as a password.
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
#include <QMovie>
#include <QLineEdit>
#include <QString>
#include <QDebug>
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Single line of text
      QLineEdit *pLineEdit=new QLineEdit(this);
      pLineEdit->setText("Set single line input box contents");
      pLineEdit->setEchoMode(QLineEdit::Password);    // Set to password mode
      QString contents=pLineEdit->text(a);qDebug()<<contents;
      pLineEdit->move(100.100);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

Now everything in the text input field has been replaced with “●”.

setCompleter()

If you want automatic association like some search engines do, you can call QLineEdit’s setCompleter() method to do it.

First of all, to realize the automatic association function, we need to define a QCompleter object, then set the content of the automatic association for it, and finally call the setCompleter method with the text input box object, and pass the QCompleter object as a parameter.

Since automatic associations tend to consist of many strings, we need to use QStringList to implement a list of strings.

P.S: Don’t forget the import and header files.

QStringList mStringList;                
mStringList<<"apple"<<"alpha"<<"above";     // This is similar to cin
Copy the code

You then define a QCompleter object, passing in a list of strings as arguments.

QCompleter *pCompleter=new QCompleter(mStringList,this);     // The second argument is the parent object
Copy the code

The QCompleter object is then loaded into the QLineEdit object.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
#include <QMovie>
#include <QLineEdit>
#include <QString>
#include <QDebug>
#include <QCompleter>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Automatic association
      QLineEdit *pLineEdit=new QLineEdit(this);
      QStringList mStringList;
      mStringList<<"apple"<<"alpha"<<"above";
      QCompleter *pCompleter=new QCompleter(mStringList,this);
      pLineEdit->setCompleter(pCompleter);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

Enter the letter A and all the associative words containing a appear.

setCaseSensitivity()

In addition, if you want the matching of associative words to be case-insensitive, you can call the setCaseSensitivity() method of QCompleter, which takes two parameters:

  1. Qt::CaseInsensitive is CaseInsensitive
  2. Qt::CaseSensitive CaseSensitive
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
#include <QMovie>
#include <QLineEdit>
#include <QString>
#include <QDebug>
#include <QCompleter>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Automatic association
      QLineEdit *pLineEdit=new QLineEdit(this);
      QStringList mStringList;
      mStringList<<"apple"<<"Alpha"<<"above";
      QCompleter *pCompleter=new QCompleter(mStringList,this);
      pCompleter->setCaseSensitivity(Qt::CaseInsensitive);    // Set to case insensitive
      pLineEdit->setCompleter(pCompleter);
 
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

In this way, the input letter a, whether it begins with or without a, will appear on the association word list.

setFilterMode()

If we want the association of the input to be not a head match, but a partial match, we can use the setFilterMode() method of QCompleter. Its parameters are:

  1. Qt::MatchExactly Performs QVariant matches (QVariant can be considered as a union of Qt’s most commonly used variable types)
  2. Qt::MatchFixedString performs character-by-character matching. Note that this method is case-insensitive by default and is case-sensitive only if Qt::MatchCaseSensitive is also specified.
  3. Qt::MatchContains Search criteria are included in the (QComboBox dropdown list) project
  4. Qt: : MatchStartsWith first match
  5. Qt: : MatchEndsWith tail matching
  6. Qt::MatchCaseSensitive is case-sensitive
  7. Qt::MatchRegExp matches by regular expression
  8. Qt::MatchWildcard 
  9. Qt::MatchWrap performs a “token ring” -like search, validating each item
  10. Qt::MatchRecursive performs recursive searches

[From: blog.csdn.net/qter_wd007/…]

The parameter we need to set here is Qt::MatchContains:

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QLabel>
#include <QMovie>
#include <QLineEdit>
#include <QString>
#include <QDebug>
#include <QCompleter>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Automatic association
      QLineEdit *pLineEdit=new QLineEdit(this);
      QStringList mStringList;
      mStringList<<"apple"<<"Alpha"<<"above";
      QCompleter *pCompleter=new QCompleter(mStringList,this);
      pCompleter->setFilterMode(Qt::MatchContains);    // Some content matches
      pCompleter->setCaseSensitivity(Qt::CaseInsensitive);
      pLineEdit->setCompleter(pCompleter);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

Enter the letter P, and anything in the string list that contains the letter P will appear in the association word candidate list.

P.S: If there is any error, welcome to correct ~