QT学习笔记2(各种组件和四大布局)
1、操作
新建一个项目:
目录文件:
2、窗口最大化、最小化和全屏
使窗口最大化、最小化和全屏:
代码:
//处理最大化按钮的事件
QPushButton * maxPushButton = new QPushButton(this);
maxPushButton->setText("最大化");
maxPushButton->setGeometry(100,100,80,30);
this->connect(maxPushButton,&QPushButton::clicked,[=](){
this->setWindowState(Qt::WindowMaximized);
});
//处理最小化按钮的事件
QPushButton * minPushButton = new QPushButton(this);
minPushButton->setText("最小化");
minPushButton->setGeometry(200,100,80,30);
this->connect(minPushButton,&QPushButton::clicked,[=](){
this->setWindowState(Qt::WindowMinimized);
});
//处理全屏按钮的事件
QPushButton * fullScreenPushButton = new QPushButton(this);
fullScreenPushButton->setText("全屏");
fullScreenPushButton->setGeometry(300,100,80,30);
this->connect(fullScreenPushButton,&QPushButton::clicked,[=](){
this->setWindowState(Qt::WindowFullScreen);
});
3、窗口无边框
使窗口无边框:
代码:
//处理关闭窗口事件
QPushButton * closePushButton = new QPushButton(this);
closePushButton->setText("关闭窗口");
closePushButton->setGeometry(230,200,80,30);
this->connect(closePushButton,&QPushButton::clicked,this,&QWidget::close);
//设置无边框窗口
setFixedSize(600,500);
setWindowFlags(Qt::FramelessWindowHint);
4、QLabel的使用
4.1、显示文字
QLabel * label = new QLabel(this);
label->setText("我是label组件");
4.2、换行显示
QLabel * label = new QLabel(this);
label->setText("我是label组件\ntest");
4.3、设置样式:字体、字体颜色、背景
setFixedSize(600,500);
QLabel * label = new QLabel(this);
label->setGeometry(200,200,100,60);
label->setText("我是label组件\ntest");
label->setStyleSheet(QString("background-color:#FF0000;"
"color:#FFFFFF;"));
label->setAlignment(Qt::AlignCenter);
4.4、显示图片、播放gif动画
4.4.1、用qss来进行设置background-image、border-image、image
setFixedSize(600,500);
QLabel * label = new QLabel(this);
label->setGeometry(0,0,600,500);
label->setText("我是label组件\ntest");
// label->setStyleSheet(QString("background-image: url(':/resource/index_logo.png') no-repeat;"));
// label->setStyleSheet(QString("image: url(':/resource/index_logo.png');"));
label->setStyleSheet(QString("border-image: url(':/resource/index_logo.png');"));
label->setAlignment(Qt::AlignCenter);
4.4.2、用QPixmap显示图片
setFixedSize(600,500);
QLabel * label = new QLabel(this);
label->setGeometry(0,0,600,500);
label->setText("我是label组件\ntest");
// label->setStyleSheet(QString("background-image: url(':/resource/index_logo.png') no-repeat;"));
// label->setStyleSheet(QString("image: url(':/resource/index_logo.png');"));
// label->setStyleSheet(QString("border-image: url(':/resource/index_logo.png');"));
label->setAlignment(Qt::AlignCenter);
QPixmap pixmap = QPixmap(":/resource/index_logo.png");
label->setWordWrap(true);
label->setPixmap(pixmap);
4.4.3、播放gif动画
setFixedSize(600,500);
QLabel * label = new QLabel(this);
label->setGeometry(0,0,600,500);
label->setText("我是label组件\ntest");
// label->setStyleSheet(QString("background-image: url(':/resource/index_logo.png') no-repeat;"));
// label->setStyleSheet(QString("image: url(':/resource/index_logo.png');"));
// label->setStyleSheet(QString("border-image: url(':/resource/index_logo.png');"));
label->setAlignment(Qt::AlignCenter);
QMovie * movie = new QMovie(":/resource/test.gif");
label->setMovie(movie);
label->show();
movie->start();
4.5、设置富文本(label->setTextFormat(Qt::RichText);)
#include "mylabel.h"
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
}
void MyLabel::enterEvent(QEvent *event){
this->setStyleSheet("background-color:#00FFFF;");
}
void MyLabel::leaveEvent(QEvent *event){
this->setStyleSheet("background-color:#FF0000;");
}
setFixedSize(600,500);
MyLabel * label = new MyLabel(this);
label->setGeometry(0,0,600,500);
label->setText("我是label组件<br/>test<br/>"
"<a href='https://www.baidu.com'>百度一下</a>");
label->setTextFormat(Qt::RichText);
label->setAlignment(Qt::AlignCenter);
label->setStyleSheet(QString("background-color:#FF0000;"
"color:#FFFFFF;"));
4.6、选择和编辑
setFixedSize(600,500);
QLabel * label = new QLabel(this);
label->setGeometry(0,0,600,500);
label->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextEditable|Qt::TextEditorInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse);
label->setText("我是一个label标签<b/>"
"<a href='https://www.baidu.com'>百度一下</a>");
label->setTextFormat(Qt::RichText);
label->setStyleSheet("background-color:#FF0000;"
"color:#FFFFFF;");
label->setAlignment(Qt::AlignCenter);
5、QPushButton的使用
5.1、事件设置
setMinimumSize(600,500);
QPushButton * closePushButton = new QPushButton(this);
closePushButton->setGeometry(230,200,100,36);
closePushButton->setText("关闭窗口按钮");
this->connect(closePushButton,&QPushButton::clicked,[=](){
this->close();
});
QPushButton * maxPushButton = new QPushButton(this);
maxPushButton->setGeometry(340,200,100,36);
maxPushButton->setText("最大化窗口按钮");
this->connect(maxPushButton,&QPushButton::released,[=](){
this->setWindowState(Qt::WindowMaximized);
});
5.2、快捷键设置
setMinimumSize(600,500);
QPushButton * closePushButton = new QPushButton(this);
closePushButton->setGeometry(230,200,100,36);
closePushButton->setText("关闭窗口按钮");
this->connect(closePushButton,&QPushButton::clicked,[=](){
this->close();
});
QPushButton * maxPushButton = new QPushButton(this);
maxPushButton->setGeometry(340,200,100,36);
maxPushButton->setText("最大化窗口按钮");
this->connect(maxPushButton,&QPushButton::released,[=](){
this->setWindowState(Qt::WindowMaximized);
});
maxPushButton->setShortcut(tr("Ctrl+E"));
5.3、样式设置
QPushButton:hover、QPushButton:!hover:
setMinimumSize(600,500);
QPushButton * closePushButton = new QPushButton(this);
closePushButton->setGeometry(230,200,200,36);
closePushButton->setText("关闭窗口按钮");
closePushButton->setStyleSheet("QPushButton:hover{"
"background-color : #FF0000;"
"color:#00FFFF;"
"font-size:30px;"
"}"
""
"QPushButton:!hover{"
"background-color : #FFFF00;"
"color:#0000FF;"
"font-size:20px;"
"}"
""
"background-color:#FFFF00;"
"color:#0000FF;"
"font-size:20px;");
6、QLineEdit的使用
6.1、属性方法
setPlaceholderText:设置输入提示
setFont:设置字体
setReadOnly:设置是否可复制
setMaxLength:设置输入字数的最大长度
setEchoMode:设置输入框的模式:Normal, NoEcho, Password, PasswordEchoOnEdit
setMinimumSize(600,500);
lineEdit = new QLineEdit(this);
lineEdit->setGeometry(230,200,200,50);
lineEdit->setPlaceholderText("请输入一行字符串");
lineEdit->setFont(QFont("微软雅黑",16));
lineEdit->setReadOnly(false);
lineEdit->setMaxLength(10);
lineEdit->setEchoMode(QLineEdit::Password);
6.2、信号事件
setMinimumSize(600,500);
lineEdit = new QLineEdit(this);
lineEdit->setGeometry(230,200,200,50);
lineEdit->setPlaceholderText("请输入一行字符串");
lineEdit->setFont(QFont("微软雅黑",16));
lineEdit->setReadOnly(false);
lineEdit->setMaxLength(3);
QPushButton * undoPushButton = new QPushButton(this);
undoPushButton->setGeometry(450,200,200,50);
undoPushButton->setText("undo操作");
this->connect(undoPushButton,&QPushButton::clicked,this,&Widget::dealClick);
6.3、qss样式
lineEdit->setStyleSheet("QLineEdit{"
"border:2px solid red;"
"border-radius:2px;"
"}"
""
"QLineEdit[echoMode='2']{"
"background-color:#CCCCCC;"
"}");
7、QObject的解读和布局类(Layout)
7.1、递归遍历子节点(children())
void Widget::listChildrens(QWidget * widget){
QObjectList objectList = widget->children();
for (int i=0;i<objectList.size();i++){
qDebug() << objectList[i]->objectName();
qDebug() << objectList[i]->metaObject()->className();
listChildrens(qobject_cast<QWidget *>(objectList[i]));
}
}
setMinimumSize(600,500);
QPushButton * pushButton = new QPushButton(this);
pushButton->setObjectName("pushButton");
QCheckBox * checkBox = new QCheckBox(this);
checkBox->setObjectName("checkBox");
QLabel * label = new QLabel(this);
label->setObjectName("label");
listChildrens(this);
7.2、Layout
setContentsMargins:设置整个布局的间隔;setSpacing:设置组件间隔
QVBoxLayout * vBoxLayout = new QVBoxLayout(layoutWidget);
vBoxLayout->setContentsMargins(0,0,0,0);
vBoxLayout->setSpacing(0);
resize在layout里不起作用,应该用setMinimumSize进行大小的设置!!!
label->setMinimumSize(100,100);
7.2.1、QVBoxLayout
setMinimumSize(600,500);
QWidget * layoutWidget = new QWidget(this);
layoutWidget->setGeometry(0,0,200,200);
layoutWidget->setStyleSheet("background-color: blue;");
QVBoxLayout * vBoxLayout = new QVBoxLayout(layoutWidget);
vBoxLayout->setContentsMargins(0,0,0,0);
vBoxLayout->setSpacing(0);
QPushButton * pushButton = new QPushButton(layoutWidget);
pushButton->setObjectName("pushButton");
pushButton->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
pushButton->setStyleSheet("background-color:green;margin:-1px;");
QCheckBox * checkBox = new QCheckBox(layoutWidget);
checkBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
checkBox->setObjectName("checkBox");
checkBox->setStyleSheet("background-color:red;margin:-1px;");
QLabel * label = new QLabel(layoutWidget);
label->setObjectName("label");
vBoxLayout->addWidget(pushButton);
vBoxLayout->addWidget(checkBox);
vBoxLayout->addWidget(label);
7.2.2、QHBoxLayout
setMinimumSize(600,500);
QWidget * layoutWidget = new QWidget(this);
layoutWidget->setGeometry(0,0,200,200);
layoutWidget->setStyleSheet("background-color: blue;");
QHBoxLayout * hBoxLayout = new QHBoxLayout(layoutWidget);
hBoxLayout->setContentsMargins(0,0,0,0);
hBoxLayout->setSpacing(0);
hBoxLayout->setAlignment(Qt::AlignLeft);
QPushButton * pushButton = new QPushButton(layoutWidget);
pushButton->setObjectName("pushButton");
pushButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
pushButton->setStyleSheet("background-color:green;margin:-1px;");
QCheckBox * checkBox = new QCheckBox(layoutWidget);
checkBox->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
checkBox->setObjectName("checkBox");
checkBox->setStyleSheet("background-color:red;margin:-1px;");
QLabel * label = new QLabel(layoutWidget);
label->setObjectName("label");
label->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
label->setMinimumSize(100,100);
label->setStyleSheet("background-color:yellow;margin:-1px;");
hBoxLayout->addWidget(pushButton);
hBoxLayout->addWidget(checkBox);
hBoxLayout->addWidget(label);
7.2.3、QGridLayout
gridLayout->setContentsMargins(0,0,0,0);
gridLayout->setVerticalSpacing(0);
gridLayout->setHorizontalSpacing(0);
gridLayout->setSpacing(0);
setMinimumSize(600,500);
QWidget * layoutWidget = new QWidget(this);
layoutWidget->setGeometry(0,0,200,200);
layoutWidget->setStyleSheet("background-color: blue;");
QGridLayout * gridLayout = new QGridLayout(layoutWidget);
gridLayout->setContentsMargins(0,0,0,0);
gridLayout->setVerticalSpacing(0);
gridLayout->setHorizontalSpacing(0);
gridLayout->setSpacing(0);
QPushButton * pushButton1 = new QPushButton(layoutWidget);
pushButton1->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
pushButton1->setMinimumSize(50,50);
pushButton1->setText("button1");
QPushButton * pushButton2 = new QPushButton(layoutWidget);
pushButton2->setMinimumSize(50,50);
pushButton2->setText("button2");
QPushButton * pushButton3 = new QPushButton(layoutWidget);
pushButton3->setMinimumSize(50,50);
pushButton3->setText("button3");
QPushButton * pushButton4 = new QPushButton(layoutWidget);
pushButton4->setMinimumSize(50,50);
pushButton4->setText("button4");
gridLayout->addWidget(pushButton1,0,0,Qt::AlignCenter);
gridLayout->addWidget(pushButton2,0,1,Qt::AlignCenter);
gridLayout->addWidget(pushButton3,0,2,Qt::AlignCenter);
gridLayout->addWidget(pushButton4,1,1,Qt::AlignCenter);
现象:内部的组件设置SizePolicy为Fixed不生效,默认会充满整个layout!!!
7.2.4、QFormLayout
setMinimumSize(600,500);
QWidget * layoutWidget = new QWidget(this);
layoutWidget->setGeometry(0,0,200,200);
layoutWidget->setStyleSheet("background-color: blue;");
QFormLayout * formLayout = new QFormLayout(layoutWidget);
formLayout->setContentsMargins(0,0,0,0);
formLayout->setVerticalSpacing(0);
formLayout->setHorizontalSpacing(0);
formLayout->setSpacing(0);
QPushButton * pushButton1 = new QPushButton(layoutWidget);
pushButton1->setMinimumSize(50,50);
pushButton1->setText("button1");
QPushButton * pushButton2 = new QPushButton(layoutWidget);
pushButton2->setMinimumSize(50,50);
pushButton2->setText("button2");
QPushButton * pushButton3 = new QPushButton(layoutWidget);
pushButton3->setMinimumSize(50,50);
pushButton3->setText("button3");
QPushButton * pushButton4 = new QPushButton(layoutWidget);
pushButton4->setMinimumSize(50,50);
pushButton4->setText("button4");
formLayout->insertRow(0,pushButton1,pushButton2);
formLayout->insertRow(1,pushButton3,pushButton4);
遍历QFormLayout:
for (int i=0;i<formLayout->rowCount();i++){
QLayoutItem * labelLayoutItem = formLayout->itemAt(i,QFormLayout::LabelRole);
QLayoutItem * fieldLayoutItem = formLayout->itemAt(i,QFormLayout::FieldRole);
QPushButton * labelPushButton = qobject_cast<QPushButton *>(labelLayoutItem->widget());
QPushButton * fieldPushButton = qobject_cast<QPushButton *>(fieldLayoutItem->widget());
qDebug() << labelPushButton->metaObject()->className();
qDebug() << fieldPushButton->metaObject()->className();
}
8、QCheckBox、QRadioButton、QButtonGroup的使用
8.1、QCheckBox
setMinimumSize(600,500);
QCheckBox * checkBox = new QCheckBox(this);
checkBox->setText("音乐");
checkBox->setGeometry(100,100,100,30);
8.2、QButtonGroup结合QCheckBox使用
setMinimumSize(600,500);
QButtonGroup * buttonGroup = new QButtonGroup(this);
QCheckBox * checkBox1 = new QCheckBox(this);
checkBox1->setText("音乐");
checkBox1->setGeometry(100,100,100,30);
QCheckBox * checkBox2 = new QCheckBox(this);
checkBox2->setText("水果");
checkBox2->setGeometry(100,140,100,30);
QCheckBox * checkBox3 = new QCheckBox(this);
checkBox3->setText("蔬菜");
checkBox3->setGeometry(100,180,100,30);
buttonGroup->addButton(checkBox1);
buttonGroup->addButton(checkBox2);
buttonGroup->addButton(checkBox3);
//设置是否为单选 true:单选 false:多选
buttonGroup->setExclusive(true);
8.3、QButtonGroup结合QRadioButton使用
setMinimumSize(600,500);
QButtonGroup * buttonGroup = new QButtonGroup(this);
QRadioButton * radioButton1 = new QRadioButton(this);
radioButton1->setText("音乐");
radioButton1->setGeometry(100,100,100,30);
QRadioButton * radioButton2 = new QRadioButton(this);
radioButton2->setText("水果");
radioButton2->setGeometry(100,140,100,30);
QRadioButton * radioButton3 = new QRadioButton(this);
radioButton3->setText("蔬菜");
radioButton3->setGeometry(100,180,100,30);
buttonGroup->addButton(radioButton1);
buttonGroup->addButton(radioButton2);
buttonGroup->addButton(radioButton3);
radioButton2->setChecked(true);
//设置是否为单选 true:单选 false:多选
buttonGroup->setExclusive(true);
信号处理:
setMinimumSize(600,500);
QButtonGroup * buttonGroup = new QButtonGroup(this);
QRadioButton * radioButton1 = new QRadioButton(this);
radioButton1->setObjectName("radioButton1");
radioButton1->setText("音乐");
radioButton1->setGeometry(100,100,100,30);
QRadioButton * radioButton2 = new QRadioButton(this);
radioButton2->setObjectName("radioButton2");
radioButton2->setText("水果");
radioButton2->setGeometry(100,140,100,30);
QRadioButton * radioButton3 = new QRadioButton(this);
radioButton3->setObjectName("radioButton3");
radioButton3->setText("蔬菜");
radioButton3->setGeometry(100,180,100,30);
buttonGroup->addButton(radioButton1,1);
buttonGroup->addButton(radioButton2,2);
buttonGroup->addButton(radioButton3,3);
radioButton2->setChecked(true);
//设置是否为单选 true:单选 false:多选
buttonGroup->setExclusive(true);
this->connect(buttonGroup,QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked),[=](QAbstractButton * button){
QRadioButton * radioButton = qobject_cast<QRadioButton *>(button);
qDebug() << radioButton->isChecked() << radioButton->objectName();
});
8、QComboBox(下拉框)
setMinimumSize(600,500);
QComboBox * comBox = new QComboBox(this);
comBox->setGeometry(200,200,200,50);
comBox->setIconSize(QSize(50,50));
comBox->addItem(QIcon(":/resource/index_logo.png"),QString("test1"),QVariant("data1"));
comBox->addItem(QIcon(":/resource/index_logo.png"),QString("test2"),QVariant("data2"));
comBox->addItem(QIcon(":/resource/index_logo.png"),QString("test3"),QVariant("data3"));
//处理信号与槽
this->connect(comBox,QOverload<int>::of(&QComboBox::activated),[=](int index){
qDebug() << index << comBox->itemText(index) << comBox->itemData(index);
});
9、QSlider(滑块)
setMinimumSize(600,500);
QSlider * slider = new QSlider(this);
slider->setRange(0,100);
slider->setGeometry(200,200,200,50);
slider->setOrientation(Qt::Horizontal);
slider->setStyleSheet("QSlider:groove{"
"background-color:red;"
"}"
""
"QSlider:handle{"
"background-color:blue;"
"width:6px;"
"}");
this->connect(slider,&QSlider::sliderMoved,[=](int pos){
qDebug() << pos;
});
9、QListWidge
9.1、基本操作
setMinimumSize(600,500);
QListWidget * listWidget = new QListWidget(this);
listWidget->setGeometry(200,50,360,300);
listWidget->setCurrentRow(10);
listWidget->setSortingEnabled(false);
listWidget->setIconSize(QSize(50,50));
//设置Ctrl多选
listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
for (int i=0;i<100;i++){
QListWidgetItem * listWidgetItem = new QListWidgetItem(QIcon(":/resource/index_logo.png"),QString("item%1").arg(i+1),listWidget);
listWidget->addItem(listWidgetItem);
}
//设置信号和槽
this->connect(listWidget,&QListWidget::itemClicked,[=](QListWidgetItem *item){
qDebug() << item->text();
});
9.2、设置listWidget里的子项可编辑
for (int i=0;i<100;i++){
QListWidgetItem * listWidgetItem = new QListWidgetItem(QIcon(":/resource/index_logo.png"),QString("item%1").arg(i+1),listWidget);
//设置listWidget里的子项可编辑
listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
listWidget->addItem(listWidgetItem);
}
9.3、遍历listWidget里的所有子项
for (int i=0;i<listWidget->count();i++){
QListWidgetItem * listWidgetItem = listWidget->item(i);
qDebug() << listWidgetItem->text();
}
9.4、对listWidget列表排序
listWidget->sortItems(Qt::DescendingOrder);
9.5、在QListWidget上添加组件
for (int i=0;i<100;i++){
QListWidgetItem * listWidgetItem = new QListWidgetItem(QIcon(":/resource/index_logo.png"),QString("item%1").arg(i+1),listWidget);
//设置listWidget里的子项可编辑
listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
listWidget->addItem(listWidgetItem);
}
listWidget->sortItems(Qt::DescendingOrder);
listWidget->sortItems(Qt::DescendingOrder);
listWidget->setItemWidget(listWidget->item(0),new QPushButton(listWidget));
9、QListWidge
9.1、设置滚动条策略
tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
9.2、添加table
setMinimumSize(600,500);
QTableWidget * tableWidget = new QTableWidget(this);
tableWidget->setGeometry(200,100,300,300);
tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
tableWidget->setColumnCount(3);
for (int i=0;i<3;i++){
QTableWidgetItem * tableWidgetItem = new QTableWidgetItem(QString("column%1").arg(i+1));
tableWidget->setHorizontalHeaderItem(i,tableWidgetItem);
}
tableWidget->setRowCount(100);
for (int i=0;i<100;i++){
for (int j=0;j<tableWidget->columnCount();j++){
QTableWidgetItem * tableWidgetItem = new QTableWidgetItem(QString("column%1").arg(j+1));
tableWidget->setItem(i,j,tableWidgetItem);
}
}
9.3、在QTableWidget上添加组件
QPushButton * pushButton = new QPushButton(tableWidget);
pushButton->setMinimumSize(200,30);
pushButton->setText("按钮组件");
tableWidget->setColumnWidth(0,300);
tableWidget->setCellWidget(0,0,pushButton);
9.4、设置行标
QStringList * stringList = new QStringList();
for (int i=0;i<100;i++){
stringList->append(QString("row%1").arg(i+1));
}
tableWidget->setVerticalHeaderLabels(*stringList);
9.5、removeRow
tableWidget->removeRow(1);
还没有评论,来说两句吧...