0.General knowledge
How qt works
QT working principle: event-driven, signal and slot mechanism
1. Event: anything that happens on the control (mouse movement, click, control drawing, movement, keyboard press, etc.)
2. Signal: It is an incomplete function (only declared but not defined). It is provided by the system class library or declared by the programmer himself in the signals area of the class body.
Signals can be triggered by events or manually by programmers themselves
3. Slot function: It is a complete function (both declared and defined), which is provided by the system class library or declared by the programmer himself in the slots area of the class body.
The essence of the slot function is a member function
4. Event driven: any event generated on the control, the control will trigger the corresponding signal or signals
5. Signal and slot mechanism: associate a signal with a slot function (binding, registration). When the signal is triggered, the system automatically calls the corresponding slot function to implement the function.
connect function;
Multiple signals can be associated with the same slot function
Partitions in widget.h
#include <QWidget>
#include <QPushButton>
//The header files of the controls used must be added.
class Widget : public QWidget
{<!-- -->
Q_OBJECT
signals: //Signal declaration area
private slots: //slot function declaration area
void btnClick(); //alt + enter can jump directly to wiget.c to write the implementation code
void btn2Click();
private: //Private members, control objects can be declared here, no need to define widget.c, can be used directly
QPushButton *btn3;
QPushButton *btn;
public:
Widget(QWidget *parent = 0);
~Widget();
};
UI interface drag and drop controls
Right-click the control in the designer and go to the slot: automatically declare and define the slot function, which has been bound to the signal.
Slot function syntax rules:
void on_objectName_singnal();
objectname: control name (object name)
The signal signal of the objectName control is triggered, and the system automatically calls the slot function
If you want to delete the function, remember to delete the declaration, otherwise an error will be reported
Signal with parameters
When the signal has formal parameters and the purpose is to trigger the signal, pass the data
For the associated slot function, if you need to receive data, you must design formal parameters of the same type; if you do not need data, you do not need to set formal parameters.
The relationship between the number of parameters of signal and slot functions:
The number of formal parameters of the slot function <= the number of formal parameters of the signal
Three ways to bind signals and slots
New style:
connect(ui->ld, & amp;QLineEdit::textChanged,this, & amp;Widget::textChange);
When the slot function is overloaded:
connect(ui->ld, & amp;QLineEdit::textChanged, this, QOverload<QString>::of( & amp;Widget::textChange));
The slot function whose parameter is QString type is selected.
When the signal is reloaded:
connect(cob,QOverload<int>::of( & amp;QComboBox::activated),this, & amp;Widget::CobPic);
The signal selected is a parameter of type int.
Old style: (not very safe)
connect(cob,SIGNAL(activated(int)),this,SLOT(CobPic(int)));
connect(ui->lineEdit_3,SIGNAL(textChanged(QString)),this,SLOT(textChange(QString)));
}
The third way of writing:
The third way to write the association between signal and slot function:
Anonymous functions (lambda expressions), for slot functions with few functional codes.
Parameter 3 this: optional
Parameter 4: Indicates that the receiver of the signal is an anonymous function [] represents the function name (): parameter list of the function {<!-- -->}: function body &: all members of the external class referenced in the anonymous function body
connect(second, & amp;SecondForm::backForm, this, [ & amp;](){<!-- -->
this->show();
second->hide();
});
Unbinding signals and slots
discount();
disconnect(ui->ld, & amp;QLineEdit::textChanged,this, & amp;Widget::textChange);
Some basic control instructions
Set window size: fixed size
setFixedSize(640,480) //Function overloading
setFixedSize(Qsize(640,480)) //An anonymous object is passed (the object name is not known)
Set window title:
setWindowTitle(QString"ikun");
Set window icon:
setWindowIcon();
The upper left corner of the control is the calculation point of the coordinates
Move the control to a certain coordinate:
move();
btn->move(QPoint(((width()-btn->width())/2),(height()-btn->height())/2));
Get the width of the control:
width();
If it is not pointed to, it is the window width: width();
Pointed to is the width of the control: btn->width()
Get the height of the control
height();
If it is not pointed to, it is the window height: height();
What is pointed to is the height of the control: btn->height();
Set control position and size:
setGeometry();
eg:btn2->setGeometry(btn->x()-120,btn->y()-80,120,80);
Get the x-axis coordinate value of the control:
x();
What is pointed to is the abscissa of the control: btn->x()
Get the y-axis coordinate value of the control
y();
What is pointed to is the abscissa of the control: btn->y()
Set up the style sheet:
setStyleSheet();
btn->setStyleSheet("border: 2px solid #B0B1B6;"
"border-top:0px;"
"border-left:0px;"
"border-right:0px;"
"background-color:#849B91");
lab2->setStyleSheet("border: 1px solid #1111ff;");
QString:
Qstring("%1 %2...%1").arg().arg()...; Assemble data into a string according to a certain format
Hidden controls:
hide()
btn->hide();
Display controls:
show()
btn->show();
Generally, everything starts with set.
Without set, the information is obtained
1. Button control
#include <QPushButton>
Three signals that buttons need to pay attention to:
clicked(bool checked)
pressed() (triggered by pressing all the time)
released() (triggered when the button is released)
Construct button:
QPushButton *btn=new QPushButton(this);
Specifying the parent object as this actually constructs a button on the current interface.
Set button text:
setText();
btn->setText("Button"); //Press after the btn pointer. Automatically appears ->
Set button size:
resize();
btn->resize(QSize(w, h));
Buttons can set icons:
setIcon();
btn2->setIcon(QIcon("1.jpg"));
Set icon size:
setIconSize();
btn2->setIconSize(QSize(60,60));
eg:
1. Declare in private slots of widget.h
void btnClick(); //The cursor alt+enter jumps to the implementation after the semicolon
void btn2Click();
2. Implement btnClik() in widget
void Widget::btnClick()
{<!-- -->
int radius=btn3->width()/2;
btn3->setStyleSheet(QString("background-color: %1;"
"border-radius: %2px").arg("red").arg(radius));
}
void Widget::btn2Click()
{<!-- -->
int radius=btn3->width()/2;
btn3->setStyleSheet(QString("background-color: %1;"
"border-radius: %2px").arg("blue").arg(radius));
}
3. Bind connection
//Associate btn's clicked signal with the btn1Click slot function of this current class object.
connect(btn, & amp;QPushButton::clicked,this, & amp;Widget::btnClick);
Trigger control. Signal triggered by the control. Current interface. Slot function implemented by signal triggering.
connect(btn2, & amp;QPushButton::clicked,this, & amp;Widget::btn2Click);
Result: When button 1 is pressed, button 3 becomes a red circle, and when button 2 is pressed, button 3 becomes a blue circle. See the code for the specific implementation.
2. Label control
#include <QLabel>
Set text alignment:
setAlignment();
lab->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
Set automatic scaling of content:
setScaledContents();
lab2->setScaledContents(true);
Show plot:
setPixmap();
lab2->setPixmap(QPixmap("1.jpg"));
Zoom while drawing:
setPixmap().scaled( , );
lab2->setPixmap(QPixmap("1.jpg").scaled(lab2->width(),lab2->height()));
eg:
Widget::Widget(QWidget *parent)
: QWidget(parent)
{<!-- -->
setFixedSize(640,480);
QLabel *lab=new QLabel("label",this);
lab->setStyleSheet("background-color: #ff11ff;");
//lab->setFixedSize(100,100);
lab->resize(QSize(100,80));
//Set text alignment
lab->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
lab->setText("ikun");
QLabel *lab2=new QLabel(this);
lab2->resize(QSize(width(),height()-lab->height()));
lab2->move(QPoint(0,lab->height()));
lab2->setStyleSheet("border: 1px solid #1111ff;");
lab2->setPixmap(QPixmap("1.jpg").scaled(lab2->width(),lab2->height()));
}
3. Line editor control
#include <QLineEdit>
Signals to watch out for:
textChanged(const QString & amp;text) (triggered when the text content changes)
textEdited(const QString & amp;text) (Triggered when the user changes the text content through editing operations (keyboard input, cut, paste, etc.), passing the edited text content.)
returnPressed() (triggered when the user presses the Enter key)
editingFinished() (triggered when editing is completed and focus moves away from QLineEdit)
Set text display mode: password mode
setEchoMode();
ui->ld2->setEchoMode(QLineEdit::Password); //Password mode
ui->ld1->setEchoMode(QLineEdit::PasswordEchoOnEdit); //Another password mode
Set prompt text:
setPlaceholderText();
ui->lineEdit_3->setPlaceholderText("Please enter your password...");
Get the text data selected by the cursor:
selectedText();
ui->label->setText(ui->ld2->selectedText());
eg:
private slots:
void textFinished();
void textFinished1();
void textFinished2();
void on_ld2_selectionChanged();
void textChange();
void textChange(QString text);
-------------------------------------------------- ----------------------------
void Widget::textFinished()
{<!-- -->
ui->label->setText(ui->ld1->text());
}
void Widget::textFinished1()
{<!-- -->
ui->label->setText(ui->ld2->text());
}
#if 1
void Widget::textFinished2()
{<!-- -->
ui->label->setText(ui->lineEdit_5->text());
}
void Widget::on_ld2_selectionChanged()
{<!-- -->
//selectedText(): Get the text data selected by the cursor
ui->label->setText(ui->ld2->selectedText());
}
void Widget::textChange()
{<!-- -->
ui->label->setText(ui->lineEdit_3->text());
}
void Widget::textChange(QString text)
{<!-- -->
ui->label->setText(text);
}
-------------------------------------------------- ----------------------------
connect(ui->ld1, & amp;QLineEdit::editingFinished,this, & amp;Widget::textFinished);
connect(ui->ld2, & amp;QLineEdit::editingFinished,this, & amp;Widget::textFinished1);
connect(ui->lineEdit_5, & amp;QLineEdit::editingFinished,this, & amp;Widget::textFinished2);
connect(ui->lineEdit_3, & amp;QLineEdit::textChanged,this,QOverload<QString>::of( & amp;Widget::textChange));
//Old style, not very safe
//connect(ui->lineEdit_3,SIGNAL(textChanged(QString)),this,SLOT(textChange(QString)));
Function:
ld1 input text is displayed on the label control
The selected text in ld2 is displayed on the label
lineEdit_5, the input text is displayed on the label control
4. Check box control
#include <QComboBox>
Signal:
currentIndexChanged(int index)
This signal is emitted when the user has selected a different item. The parameter index represents the index of the currently selected item.
currentTextChanged(const QString & amp; text)
This signal is emitted when the user selects a different item and the item contains text. The parameter text is the text of the currently selected item.
activated(int index)
This signal is emitted when the user selects an item and presses the Enter key.
activated(const QString & amp; text)
This signal is emitted when the user selects an item and presses the Enter key, if the item contains text.
Checkbox enabled:
setEditable();
cob->setEditable(true);
Add drop-down items:
addItem(): Add drop-down item
cob->addItem(edit->text());
Get the number of drop-down items in the combo box
count();
for(int i=0;i<box->count();i + + )
Get the current option index
currentIndex()
box->currentIndex();
Get the current option text
currentText()
box->currentText();
Get the text data of the drop-down item
itemText();
box->itemText(i); //Get the text data of the i-th drop-down item
Find whether the drop-down box text exists
findText(); //Method to find the index of the given text in the drop-down list
//His return value is the index where the found text is located. If no matching text is found, the return value is -1
if(cob->findText(ld->text())==-1)
{<!-- --> //If there is no repeated text, add it to the drop-down box item
cob->addItem(ld->text());
}
Click the text in the checkbox, and the image corresponding to the text will be displayed on the label:
1. Old style
Slot function:
void Widget::CobPic(int index)
{<!-- -->
lab->setPixmap(QPixmap(cob->itemText(index)).scaled(lab->width(),lab->height()));
}
Binding:
connect(cob,SIGNAL(activated(int)),this,SLOT(CobPic(int)));
-------------------------------------------------- ----------------------------------
2. New style
Index returned by activated:
Slot function:
void Widget::CobPic(int index)
{<!-- -->
lab->setPixmap(QPixmap(cob->itemText(index)).scaled(lab->width(),lab->height()));
}
Binding:
connect(cob,QOverload<int>::of( & amp;QComboBox::activated),this, & amp;Widget::CobPic);
-------------------------------------------------- ----------------------------------
3. New style
The const QString & text type returned by activated (selected text)
Slot function:
void Widget::CobPic(const QString text)
{<!-- -->
lab->setPixmap(QPixmap(text).scaled(lab->width(),lab->height()));
}
Binding:
connect(cob,QOverload<const QString & amp;>::of( & amp;QComboBox::activated),this, & amp;Widget::CobPic);
-------------------------------------------------- ----------------------------------
result:
Click the text in the checkbox, and the image corresponding to the text will be displayed on the label: