In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Why should Q_OBJECT be added to the class in Qt? it turns out that this is a macro defined by Qt. What is its function?
It turns out that this macro contains the most important mechanism in Qt: the mechanism of signals and slots. All Qt programs must use this mechanism!
Next we can first look at a simple Mini Program, a Mini Program similar to the picture loader.
Here is an unlaid-out image simply dragged by a Qt designer:
I dragged a QLabel box, two QPushButton, and changed their object names.
Here is the code in picture.cpp: I will explain it one by one.
# include "picture.h" picture::picture (QWidget * parent): QWidget (parent) {ui.setupUi (this); initSeaList (); initCliffList (); ui.label- > setScaledContents (true); this- > setWindowTitle (QString::fromLocal8Bit ("picture tour"); connect (ui.seaButton, SIGNAL (clicked ()), this, SLOT (showSeaPictureSlot ()); connect (ui.cliffButton, SIGNAL (clicked ()), this, SLOT (showCliffPictureSlot () } picture::~picture () {} void picture::showSeaPictureSlot () {ui.label- > setPixmap (QPixmap (roomList [roomNumber% 10]); seaNumber++;} void picture::initSeaList () {for (int I = 0; I)
< 10; ++i) { QString path = "Pixmap/sea/" + QString::number(i) + ".jpg"; seaList.push_back(path); }}void picture::showCliffPictureSlot(){ ui.label->SetPixmap (QPixmap (cliffList [cliffNumber% 10]); cliffNumber++;} void picture::initCliffList () {for (cliffList I = 0; I)
< 10; ++i) { QString path = "Pixmap/cliff/" + QString::number(i) + ".jpg"; cliffList.push_back(path); }}123456789101112131415161718192021222324252627282930313233343536373839404142434445 第1行:包含了"picture.h"文件,不用多解释吧. 第6行:picure的构造函数中,ui.setupUi(this);用来调用Qt设计师中通过拖拽而形成的页面. 第7.8行:对于两个StringList的初始化,也就是把图片的相对路径放到容器中,方便以后调用. 第9行:ui.label->SetScaledContents (true); the function of this member function is to fill in the label box, for example, if you put a picture inside, but the size of the picture is smaller than the label box, then there will be some extra white space in the label box, which looks ugly, and after calling this function, you can let the picture fill the label box.
Line 10: this- > setWindowTitle (QString::fromLocal8Bit ("Picture Tour"); first of all, through the this pointer to set the current title as "Picture Tour", but due to Qt coding problems, it will be garbled if it is displayed in Chinese, so with the fromLocal8Bit () function, it is static, so you can call it directly with the class name QString.
Line 11.12: by calling QObject::connect (). This connection function, come on, QPushButton and Label connect, if QPushButton is clicked, it will send out a clicked () signal, so it will call a custom showSeaPictureSlot function, which reflects the signal and slot mechanism. It is tantamount to connecting the two parts and forming a relationship.
InitSeaList function: first I put 10 images in a folder, and then I put them in the Pixmap file. Here is my name for 10 pictures.
Then put the Pixmap file here:
In particular, if you want to call external pictures or movie resources in the program, it is best to put it with the qrc file. This makes it easy to find the path. (the VS2013+Qt5.2 version used by the editor, if you can't build this environment, you can see my first post.)
In the initSeaList () function: QString is similar to string in STL and can add strings directly, while the path of the resource file starts from the qrc file. So QString path = "Pixmap/sea/" + QString::number (I) + ".jpg"; QString::number () is also a static function of QString, which can convert numbers into strings, which is very convenient.
Then call seaList.push_back (path); just put the path of the picture in.
In the showCliffPictureSlot () function: label- > setPixmap (QPixmap ()), this function sets a picture in the label box, and QStringList is similar to vector in STL. So it is also overloaded [] and can be called directly. Save the serial number of the last time with cliffNumber, you can easily visit the next one, and the function of cliffNumber is to prevent the serial number from exceeding the size of the container. Similar to the previous practice of circular queues.
Here is the code for picture.h:
# ifndef PICTURE_H#define PICTURE_H#include # include "ui_picture.h" # include # include # include class picture: public QWidget {Q_OBJECTprivate slots: void showSeaPictureSlot (); void showCliffPictureSlot (); public: picture (QWidget * parent = 0); ~ picture (); void initSeaList (); void initCliffList (); private: Ui::pictureClass ui; int seaNumber = 0; int cliffNumber = 0; QStringList seaList; QStringList cliffList;}; # endif / / PICTURE_H1234567891011121314151617181920212223242526272829303132
Here is a demonstration of running and compiling.
Summary:
QOBJECT macros have the function of connecting signals and macros. Once there is no macro definition in the class, the connection between signals and slots will disappear. Interested friends can do a test, and you can comment out this macro. You will find that the program can be compiled and passed normally and run successfully, but after I click the button, the picture will not be displayed. The reason is the lack of this macro!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.