Qt基本控件使用

Qt基本控件及三大布局

Qt基本模块
一、Qt的三大布局

QHBoxLayout: 
    水平显示布局,所有在其上面摆放的控件只能水平排列下去;
QVBoxLayout: 
    垂直显示布局,所有在其上面摆放的控件只能垂直排列下去;
QGridLayout 
    格子显示布局,可以按照表格的形式显示布局;

二、Qt的控件

label:标签,可以显示文本信息,只读;
pushbutton : 普通按钮;
radiobutton : 单选按钮,多个单选按钮中只能选择一个,但是必须放入groupbox中,类似单选题;
checkbox : 多选复选按钮,可以选择同时选择多个,类似多选题;
lineedit : 单行文本编辑框,可以输入单行文本;
textedit : 多行文本输入框,可以输入显示多行文本和图片;
combobox : 下拉文本输入框,在输入框的最右边有个三角下拉按钮,可以选择输入,也可以手动输入;
textbrower : 多行文本显示框,只读;
groupbox : 可以在里面放入特点的东西,统一管理;
slider : 模拟显示滑动条;
spinbox : 数值显示滑动条;
dateedit :
timeedit :
datetimeedit :
lcdnumber :

三、Qt的信号槽

在Qt中所有的对象(继承QObject类)都有connect函数,只要有这个函数就能建立信号槽(通过触发某个控件的信号函数,执行槽中相应的函数);(暂时这样理解,还是有点理解不全面的,之后学习到再来修改);
在Qt中信号槽中可以使用自带的函数,四个参数;也可以执行自定义的函数,三个参数;具体看下面test4的例子就明白了。

#include <QtWidgets/QApplication>
#include <QtCore/qdebug.h>
#include <QtWidgets/qcompleter.h>
#include <QtWidgets/qlineedit.h>
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qcombobox.h>
#include <QtWidgets/qcheckbox.h>
#include <QtWidgets/qradiobutton.h>
#include <QtWidgets/qtextedit.h>
#include <QtWidgets/qtextbrowser.h>
#include <QtWidgets/qgroupbox.h>
#include <QtWidgets/qslider.h>
#include <QtWidgets/qspinbox.h>
#include <QtWidgets/qdatetimeedit.h>
#include <QtWidgets/qtabwidget.h>
#include <QtWidgets/qlcdnumber.h>
#include <QtGui/qpixmap.h>
#include <QtCore/qobject.h>
#include <QtWidgets/qboxlayout.h>
#include <QtWidgets/qpushbutton.h>
#include “MyWidgetEvent.h”

void test() ;

int main(int argc, char *argv[]) {

QApplication a(argc, argv);

test() ;

return a.exec();

}

void test(){

}

void test5ManyKongJian() {

QWidget *nanWidget = new QWidget() ;
QVBoxLayout *nanVLayout = new QVBoxLayout() ;

/*
**测试label控件
*/
QLabel *label=nullptr ;
nanVLayout->addWidget( label = new QLabel("<a href=www.baidu.com>baidu</a>") ) ;
//QPixmap me("./me.png") ;
//label->setPixmap( me ) ;//问题:链接和图片重复了,怎么分开
label->setWordWrap( true ) ;
label->adjustSize() ;
nanVLayout->connect( label , &QLabel::linkActivated , []( QString str){
    qDebug()<<str ;
} ) ;
/*
**测试lineedit控件
*/
QLineEdit *lineEdit ;
nanVLayout->addWidget( lineEdit = new QLineEdit("hello") ) ;
/*
**测试button控件
*/
QPushButton *button ;
nanVLayout->addWidget( button = new QPushButton("???") ) ;
button->setStyleSheet("QPushButton {font:bold 16px; color:red;padding:5px}") ;
nanWidget->connect( button , &QPushButton::clicked , [](bool flag){
    qDebug()<< "button" ;
}) ;
/*
**测试radiobutton控件
*/
QRadioButton *radioButton ;
nanVLayout->addWidget( radioButton = new QRadioButton("qradiobutton") ) ;
radioButton->setStyleSheet("QRadioButton {font:bold  16px;color:blue;padding:5px}") ;
radioButton->connect( radioButton , &QRadioButton::clicked , [](bool flag){
    qDebug()<< flag ;
}) ;
/*
**测试ckeckbox控件
*/
QCheckBox *check ;
nanVLayout->addWidget( check = new QCheckBox("chekcbox") ) ;
/*
**测试combobox控件
*/
QComboBox *combobox ;
nanVLayout->addWidget( combobox = new QComboBox() ) ;
combobox->addItem("select item1") ;
combobox->addItem("select item2") ;
combobox->addItem("...  ...") ;
combobox->setEditable(true) ;
lineEdit->setText("start") ;
combobox->connect( combobox , SIGNAL(activated(const QString &)) , lineEdit , SLOT(setText(const QString &)) ) ;//这里的下标要跟着显示
combobox->setCompleter( new QCompleter(combobox->model()) ) ;
/*
**测试textedit
*/
QTextEdit *textEdit ;
nanVLayout->addWidget( textEdit = new QTextEdit("textedit") ) ;
textEdit->setText("<table border=2> "
                "<tr><td>good</td><td>good</td><td>good</td></tr>"
                "<tr><td>nice</td><td>nice</td><td>nice</td></tr>"
                "</table>"
                "<img src=./me.png></img>") ;
textEdit->connect( textEdit , &QTextEdit::textChanged,[&](){
    //问题:textEdit->toPlainText() ;怎么才能获取到textEdit的实体,this->sender()
    //QTextEdit _edit = (QTextEdit *)QObject::sender();
    qDebug()<<textEdit->toPlainText() ;
}) ;
textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded) ;
textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn) ;
/*
**测试groupbox控件
*/
QGroupBox *groupBox ;
nanVLayout->addWidget( groupBox = new QGroupBox("groupbox") ) ;
QHBoxLayout *hLayout ;
groupBox->setLayout( hLayout = new QHBoxLayout() ) ;
hLayout->addWidget( new QRadioButton("boy") ) ;
hLayout->addWidget( new QRadioButton("girl") ) ;
/*
**测试模拟显示条slider
*/
QSlider *slider ;
nanVLayout->addWidget( slider = new QSlider() ) ;
slider->setMinimum( -100 ) ;
slider->setMaximum( 100 ) ;
/*
**测试数字显示条
*/
QSpinBox *spinBox ;
nanVLayout->addWidget( spinBox = new QSpinBox() ) ;
spinBox->setMinimum( -100 ) ;
spinBox->setMaximum( 100 ) ;
slider->connect( slider , SIGNAL(valueChanged(int)) , spinBox , SLOT(setValue(int)) ) ;
spinBox->connect( spinBox , SIGNAL(valueChanged(int)) , slider , SLOT(setValue(int)) ) ;
/*
**测试时间设置
*/
nanVLayout->addWidget( new QDateTimeEdit() ) ;
/*
**测试显示时间,只读
*/
QLCDNumber *number ;
nanVLayout->addWidget( number = new QLCDNumber() ) ;
number->display("1314") ;
number->setMode(QLCDNumber::Hex) ;
number->setSegmentStyle(QLCDNumber::Filled) ;

nanWidget->setLayout( nanVLayout ) ;
nanWidget->setWindowTitle("control") ;
nanWidget->show() ;

}

void test4GridAndHBox() {

QWidget *nanWidget = new QWidget() ;
QGridLayout *nanGridLayout = new QGridLayout() ;
QHBoxLayout *nanHBoxLayout = new QHBoxLayout() ;

nanGridLayout->addWidget( new QLabel("Username") , 1 , 1 ) ;
nanGridLayout->addWidget( new QLineEdit() , 1 , 2 ) ;
nanGridLayout->addWidget( new QLabel("Username") , 2 , 1 ) ;
nanGridLayout->addWidget( new QLineEdit() , 2 , 2 ) ;
nanGridLayout->addLayout( nanHBoxLayout , 3 , 2 ) ;

nanHBoxLayout->addStretch(1) ;
nanHBoxLayout->addWidget( new QPushButton("Login") , 1 ) ;

nanWidget->setLayout( nanGridLayout ) ;
nanWidget->show() ;

}

void test3GridLayout() {

QWidget *nanWidget = new QWidget() ;
QGridLayout *nanLayout = new QGridLayout() ;
QPushButton *nanButton = new QPushButton() ;
QLineEdit *nanLineEdit = new QLineEdit() ;

nanLayout->addWidget( nanLineEdit , 1 , 1 , 1 , 2 ) ;
nanLayout->addWidget( new QPushButton , 1, 3 ) ;
nanLayout->addWidget( new QLineEdit , 2, 1 , 2 , 1  ) ; 
nanLayout->addWidget( new QPushButton , 2, 2 ) ;
nanLayout->addWidget( new QPushButton , 2, 3 ) ;
nanLayout->addWidget( nanButton , 3 , 3 ) ;
nanLayout->setColumnStretch( 1 , 1 ) ;
nanLayout->setColumnStretch( 2 , 1 ) ;/*设置每列的比重*/
nanLayout->setColumnStretch( 3 , 1 ) ;

nanWidget->setLayout( nanLayout ) ;
nanWidget->show() ;

}

void test2HBoxLayout() {

QWidget *nanQWidget = new QWidget() ;

QLineEdit *nanQLineEdit = new QLineEdit() ;

QHBoxLayout *nanHLayout = new QHBoxLayout() ;
nanHLayout->addWidget( nanQLineEdit , 1 ) ;
//添加,两个控件之间的距离addspaceing,两个控件在layout中的比重addstretch

nanQWidget->setLayout( nanHLayout ) ;
nanQWidget->show() ;

}

void test1(){

QWidget *w  = new QWidget ;
QVBoxLayout *vLayout = new QVBoxLayout( ) ;
QPushButton *nanButton ;
QLineEdit *nanLineEdit ;
QLabel *nanLabel ;
QString content("null") ;
QCompleter nanQCompleter( QStringList()<<"nich"<<"chen"<<"good") ;

vLayout->addWidget( nanLineEdit = new QLineEdit()  ) ;
vLayout->addWidget( nanButton = new QPushButton("right") ) ;
nanQCompleter.setFilterMode( Qt::MatchFlag::MatchContains ) ;
nanLineEdit->setCompleter( &nanQCompleter ) ;
nanLineEdit->setPlaceholderText( "Please input your name" ) ;
vLayout->addWidget(  nanLabel = new QLabel() ) ;
nanLabel->setText( content ) ;
w->connect( nanButton , &QPushButton::clicked , [&](){
    nanLabel->setText( nanLineEdit->text() ) ;
} ) ;

w->setLayout( vLayout) ;
w->show() ;

}

一、dialog、widget、mainwindow的区别

1)、dialog有exec函数,如果是dialog窗口,后边的窗口时不可选的; 
2)、widget和dialog都有show函数,如果通过这个函数显示这两种类型的窗口,则两个窗口都是可选的; 
3)、widget主要是在上面放置布局和控件; 
4)、mainwindow可以显示菜单,工具栏,状态栏、托盘等功能。

二、dialog窗口

这个dialog窗口只是为了给人们提供更好的可视化操作,但是对于程序员而言,这个操作并不是立刻执行的;而是当在窗口选择关闭后,才将选择的结果返回给后台,后台才可以根据选择的结果进行相应的操作。

#include “mydialog.h”

mydialog::mydialog(QDialog *parent):QDialog(parent) {

QPushButton *button = new QPushButton( "button" , this ) ;
connect( button , SIGNAL(clicked()) , this , SLOT(slotButtonClick()) ) ;

}

void mydialog::slotButtonClick() {

#if 0
/dialog和widget的区别,exec和show的区别而已/
QDialog *dlg = new QDialog ;
QPushButton *button = new QPushButton(“close” , dlg ) ;
connect( button , SIGNAL(clicked()) , dlg , SLOT(reject()) ) ;
int ret = dlg->exec( ) ;
//通过exec显示出来的窗口称为,模块对话框
// 在模块对话框中,exec有自己的消息循环,并且把app的消息循环接管了
// 如果Dialog是通过exec来显示,那么可以通过accept或者reject来关闭窗口
// 如果Dialog是通过show来显示,那么可以通过close来关闭窗口,这个和QWidget一样的

// 有许多特殊的dailog:文件选择,MessageBox,颜色选择,字体选择,打印预览,打印
if( ret == QDialog::Accepted ) 
    qDebug()<<"accepted" ;
else if( ret== QDialog::Rejected ) 
    qDebug()<<"rejected" ;

#endif
#if 0
/文件选择:这个窗口可以选择保存文件的名称,然后将路径+名称返回,我们就可以根据返回路径名来保存文件。/
QString strFilename = QFileDialog::getSaveFileName(NULL,
“Select file for save”,
_strDir,
“pic file (*.png .jpg)");
#endif
#if 0
/文件选择:选择要打开的文件名(绝对路劲);我们就可以根据这个文件路径来打开相应的文件/
QString strFilename = QFileDialog::getOpenFileName(NULL,
“Select file for open”,
_strDir,
"pic file (
.png *.jpg)”);
#endif
#if 0
QString strFilename = QFileDialog::getExistingDirectory();

if(strFilename.isEmpty())
{
    qDebug() << "select none";
    return;
}

qDebug() << strFilename;
QFileInfo fileInfo(strFilename);
_strDir = fileInfo.filePath();
qDebug() << _strDir;

#endif
//do something for io … …
//上面的选择将绝对路径名都给拿下来了,如果要进行保存,不是很容易吗!
QPixmap pixmap(this->size()) ;
QPainter painter(&pixmap) ;
this->render( &painter ) ;
pixmap.save(strFilename) ;
#if 0
/颜色选择对话框:可以选择相应的颜色;然后将选择的颜色返回,这样我们就可以操作了/
QColorDialog colorDia ;
colorDia.exec() ;
QColor c = colorDia.selectedColor() ;
#endif
#if 0
/字体选择对话框:可以选择字体;然后将选择的字体信息返回,我们同样可以用这些信息来设置相应的值/
QFontDialog fontDia ;
fontDia.exec() ;
QFont font = fontDia.selectedFont() ;
#endif
#if 0
/这个也是弹窗对话框,不过只是简单的选择以下枚举中的值,可以尝试下效果/
int ret = QMessageBox::question(this, “???”, “realy do …”,
QMessageBox::Yes| QMessageBox::No|
QMessageBox::YesAll| QMessageBox::NoAll);
if(ret == QMessageBox::Yes)
{
qDebug() << “user select yes”;
}
if(ret == QMessageBox::No)
{
qDebug() << “user select no”;
}
#endif
}

void mydialog::paintEvent( QPaintEvent *ev ) {
}

mydialog::~mydialog(void) { }

三、widget窗口

前面已经介绍过很多继承这个窗口的控件了,这里就不再累述。

四、mainWindow窗口

这个也是给人们提供更好的可视化操作; 
一个正常window软件呈现给客户的可视化界面; 
包括:menu菜单、tool工具栏、status状态栏、电脑显示屏右下脚的托盘等。

#include “MyWindow.h”

MyWindow::MyWindow(QMainWindow *parent):QMainWindow( parent ) {

/*menuBar菜单栏,菜单menu*/
QMenuBar  *menuBar = this->menuBar() ;
_menu = menuBar->addMenu( "&File" ) ;
QMenu *edit = menuBar->addMenu( "&Edit" ) ;

/*menu菜单中的选项action*/
QAction *openAction = _menu->addAction( "&Open"
    , this , SLOT(slotOpen()) , QKeySequence::Open  ) ;
QAction *saveAction = _menu->addAction( "&Save"
    , this , SLOT(slotOpen()) , QKeySequence::Save  ) ;
_menu->addSeparator() ;//添加分界线
QAction *closeAction = _menu->addAction( "&Exit"
    , this , SLOT(close()) , QKeySequence::Close  ) ;

/*toolBar工具栏*/
QToolBar *toolBar = this->addToolBar( "mimi" ) ;
toolBar->addAction( openAction ) ;
toolBar->addAction( saveAction ) ;
toolBar->addAction( closeAction ) ;
/*statusBar状态栏*/
QStatusBar *statusBar = this->statusBar() ;
QLabel *label ;
statusBar->addWidget( label = new QLabel("Ok") ) ;
label->setText( "<font color=blue>XXXXX... ...</font>" ) ;
/*上面的三种栏介绍完之后,剩下的窗口区域就是CentralWidget
**如果将widget直接add到mainwindow这个窗口的话,
**toolbar是会跟添加进来的widget重叠的*/
MyView *view = new MyView ;
this->setCentralWidget( view ) ;
/*最后就是window系统右下脚的托盘:system tray icon*/
QSystemTrayIcon *icon = new QSystemTrayIcon ;
icon->setIcon( QIcon("./1.png") ) ;//图标
icon->setToolTip( "luck dog" ) ;//鼠标滑过提示文字
icon->show() ;//展示在右下角
icon->setContextMenu( _menu ) ;//右击出现的菜单
this->connect( icon , SIGNAL( slotActivated(QSystemTrayIcon::ActivationReason) ) 
    , this , SLOT(slotActivated(QSystemTrayIcon::QSystemTrayIcon::ActivationReason)) ) ;
this->installEventFilter(this);

}

void MyWindow::slotActivated(QSystemTrayIcon::ActivationReason reason){

/*这个没成功*/
if( reason==QSystemTrayIcon::Trigger ) {
    if( this->isHidden() ) 
        this->show() ;
    else
        this->hide() ;
}

}

bool MyWindow::eventFilter(QObject *o, QEvent e)
{
/实现什么功能呢?/
if(o == (QObject
)this && e->type() == QEvent::Close)
{
return true;
}

return QMainWindow::eventFilter(o, e);

}

void MyWindow::slotOpen() {

QString fileName = QFileDialog::getOpenFileName() ;
qDebug()<<fileName ;
/*将打开的文件中的内容显示在窗口上... ...*/

}
bool MyWindow::event(QEvent *ev)
{
qDebug() << ev;
if(ev->type() == QEvent::Close) {
return false;
}
/怎么弄才能实现窗口关闭,托盘还在?/
return QMainWindow::event(ev);
}

void MyWindow::paintEvent( QPaintEvent * ) {

QPainter painter(this) ;
painter.drawPixmap( QPoint(0,0) , QPixmap("./1.png") ) ;

}

void MyWindow::mousePressEvent( QMouseEvent *ev ) {

if( ev->button() == Qt::RightButton ) {
    _menu->exec( QCursor::pos() ) ;
}

}

MyWindow::~MyWindow(void) { }

一、qDebug()函数

qDebug()函数可以直接输出调试错误信息,方便程序员调试信息,查找错误;
例子:qDebug()<<"error"<<endl;

二、QDebug类

这个函数可以收集错误信息,通过QTextStream这个类(之前在写文件的时候,用过这个类,查看了下函数的作用,果真和自己想的一样);
QTextStream类,指定一个Qfile*或是QString*作为参数,重载了<<操作符,将对象(QTextStream<<。。)收集到的数据都保存入,相应的参数(这个参数由构造函数决定)中,方便统一管理。例如:在构造时如果指定了文件指针,就直接保存如文件;在构造时如果指定了string,就直接保存如string中… …
总结:这个错误类不是用来输出信息的(当然简介使用错误函数也是可以输出信息),更重要的是用来收集错误信息,方便统一管理,保存,查看的。

QString *str ;
QDebug q( str = new QString("object") ) ;
q<<"nice";
qDebug()<< *str ;

1、按钮控件:QPushButton

1)新建:

QPushButton *btn = new QPushButton(“这是按钮”); //也可指定父窗口,一般默认为本窗口

2)设置固定大小:即即使窗口的大小发生变化,按钮控件的大小始终保持在固定大小,

btn->setFixedSize(100,200); //设置按钮的大小为100 x 200

3)设置背景图片:a) 首先需要添加图片:右键点击工程,选择“添加新文件”->“Qt Resource File”,点击“choose”,会出现如下图所示界面:

在该界面中,点击“添加”->“添加前缀”,然后再点击“添加”->“添加文件”,选择要添加的图片即可(.png)格式,如下图所示:

当添加完图片之后,就可以通过StyleSheet来设置背景图片了:

btn->setStyleSheet(“background-image:url(:/new/icon/res/SimStart.png)”); //其中,url即为在资源文件中所在的位置

4)设置提示文字:当鼠标停在控件上时,会弹出提示文字:

btn->setToolTip(“这是一个按钮”);

2、文本框控件:QTextEdit

1)新建:

QTextEdit *textEdit = new QTextEdit();

2)显示多行数据:

textEdit->insertPlainText(“显示信息”+"\n"); //当显示完信息之后,会换行

3、时间控件:QDateTime

1)获取时间:

QDateTime *currentTime = new QDateTime(QDateTime::currentDateTime());

2)显示时间:

QString currentTimeStr = currentTime->time().toString();

4、表格控件:QTableWidget

1)新建:

QTableWidget table = new QTableWidget(20,3); //建立了一个203的表格

2)设置表头:

QStringList tableHeader;
tableHeader << "列1" << "列2" << "列3";
table->setHorizontalHeaderLabels(tableHeader);

3)去掉自带的行号:

QHeaderView *headerViewRow = table->verticalHeader();
headerViewRow->setHidden(true);

4)使表格的宽度自适应表格控件的宽度:

table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //设置表格填充控件
table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); //其中函数中的第一个参数是用于指定某一列的宽度自适应内容,
 
//上面两个可以一起使用

5)操作表格内容:

/****** 插入一行数据 *******/
item_0 = QTableWidgetItem("Text1")
item_2 = QTableWidgetItem("Text2")
 
table->setItem(0, 0, item_0);    //插入第0行,第0列单元格的数据
table->setItem(0, 1, item_1);    //插入第0行,第1列单元格的数据
 
/****** 获取一行数据 *******/
item_0 = table->item(0, 0);
item_1 = table->item(0, 1);
 
text0 = item_0->text();        //获取第0行,第0列单元格的数据
text1 = item_1->text();        //获取第0行,第1列单元格的数据
 
/****** 删除一行数据 *******/
table->removeRow(0);            //删除第0行所有的数据
 
/****** 插入一行数据 *******/
table->insertRow(1);            //在第1行位置,插入一行
 
/****** 清空表格中所有数据 *******/
table->clearContents();

5、树控件:QTreeWidget

1)新建:

QTreeWidget *tree = new QTreeWidget(*parent);    //可以指定父控件,
tree->clear();    //初始化树

2)设置树的标题:

tree->setHeaderLabel(“这是树”); //当然也可以隐藏树的标题

3)建立树的节点:

QTreeWidgetItem *rootItem = new QTreeWidgetItem(tree);    //添加节点,其中参数表示该节点的父节点
rootItem ->setText(0, fedFileNameStr);    //设置该节点的名称
rootItem ->setWhatsThis(0, IS_ROOT);    //设置该节点的识别码,即What This Is? 可以通过该识别码判断节点是哪一类型的
rootItem ->setExpanded(true);                //设置自动展开

4)添加右键点击事件:

tree->setContextMenuPolicy(Qt::CustomContextMenu);  //必须要加,否则右键点击弹出菜单无反应
connect(tree,SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(SlotToClickRightItem(QPoint)));
 
//其中,SlotToClickRightItem(QPoint)为右键点击响应函数
currentTreeItem = tree->itemAt(clickPos);    //通过点击的位置判断点击的是树的哪一个节点,如果currentTreeItem 为空,则表示点击在空白处
 
//点击空白处,弹出右键菜单
QMenu *menuNewFed = new QMenu(this);    //创建菜单
menuNewFed->addAction(actNewFed);    //添加菜单项
menuNewFed->exec(QCursor::pos());    //将弹出菜单显示在鼠标点击处

5)添加左键点击事件:

connect(tree,SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(SlotToClickTreeItem(QTreeWidgetItem*,int)));
 
//其中SlotToClickTreeItem(QTreeWidgetItem*,int))为左键点击响应函数,其中QTreeWidgetItem*参数表示的是点击的树的节点元素,
//int型参数表示树节点元素的列数,通过获取节点元素的whatThis(colum)来判断点击的是哪一种节点元素

6、LED控件控件:QLCDNumber

1)新建:

QLCDNumber *lcd = new QLCDNumber();

2)设置属性:

lcd ->setFixedHeight(23);                 //设置控件 lcd 的宽度为23(固定宽度)
lcd ->setSegmentStyle(QLCDNumber::Flat);  //只有当设置为 flat 风格时,设置颜色才会生效
lcd ->setStyleSheet("color:yellow; background:black;");

3)显示数字:

lcd->display(“display”); //需是数字类型的字符串

7、分割窗口:QSplitter

1)建立分割窗口:

//参数中,第一个参数表示分割窗口的方式,是水平分割还是垂直分割?第二个参数表示的是要被分割的窗口。
 
QSplitter *splitterMain = new QSplitter(Qt::Vertical, this);
splitterMain->addWidget(QWidget* widget);        //将控件 widget 添加到 splitterMain 分割窗口中//左中部分隔窗口
QSplitter *splitterLeftCenter = new QSplitter(Qt::Horizontal, splitterMain);
splitterLeftCenter->addWidget(QWidget* widget); //将控件 widget 添加到 splitterLeftCenter分割窗口中
 
//右中部分隔窗口
QSplitter *splitterRightCenter = new QSplitter(Qt::Vertical, splitterLeftCenter);
splitterRightCenter->addWidget(QWidget* widget); //将控件 widget 添加到 splitterRightCenter 分割窗口中
 
 //下部分隔窗口
QSplitter *splitterLeftDown = new QSplitter(Qt::Horizontal, splitterMain);
splitterLeftDown->addWidget(QWidget* widget); //将控件 widget 添加到 splitterLeftDown 分割窗口中

2)设置分割窗口的比例

//第一个参数表示的是分割窗口的ID,第二个窗口表示的是比例大小
 
splitterMain->setStretchFactor(0, 1);       //设定上、中、下三部分的比例为1:3:1
splitterMain->setStretchFactor(1, 3);
splitterMain->setStretchFactor(2, 1);
 
splitterLeftCenter->setStretchFactor(0, 2); //设定中间部分的左、右部分的比例为2:1
splitterLeftCenter->setStretchFactor(1, 2);

3)设置分割条是否可以拖动

//用于指定分隔条是否可以拖动,其中 handle(1) 表示第1条分隔条不可拖动
QSplitterHandle *splitterMainHandle = splitterMain->handle(1);
if(splitterMainHandle)
{
    //Disable the Middle Line, it can't adjust.
    splitterMainHandle->setDisabled(true);
}

4)设置分割条的样式

splitterMain->setStyleSheet("QSplitter::handle{background-color:white}");   //设置分割条样式
splitterMain->setHandleWidth(5);                                            //设置分割条宽度

5)设置分割窗口可变

//添加一个布局,使分隔窗口随着控件一起变化
mainLayout = new QGridLayout(this);
 
mainLayout->addWidget(splitterMain, 0, 0);

6)显示

//显示
splitterMain->show();

8、堆栈窗口:QStackedWidget

1)新建:

QStackedWidget *stackWindow = new QStackedWidget(this);

2)添加窗口:

stackWindow->addWidget(QWidget *widget); //其中widget表示建立的窗口控件

3)显示窗口:

stackWindow->setCurrentIndex(ID); //其中ID表示堆栈窗口中子窗口的ID号,ID号与添加子窗口的顺序有关,从0开始

9、标签控件:QLabel

1)、新建

QLabel* label = new QLabel("标签 ");

2)、设置字体

QFont font("Times", 20);    //第一个参数为字体格式,第二个参数为字体大小
label->setFont(font);

10、弹簧控件:QSpacerItem

现有一个见面如下所示:

想要变成这样:,即,前一个图虽然 setSpacing()了,但是在调整窗口代销时,还是会出现spacing发生变化并且不是很整齐的情况,这是就需要QSpacerItem控件了,该控件是不显示在窗口中的。

1)、新建

QSpacerItem* spacer = new QSpacerItem(20,20, QSizePolicy::Minimum, QSizePolicy::Expanding);

2)、设置

frameLayout->addWidget(label1); 
frameLayout->addWidget(text1);
frameLayout->addWidget(label2);
frameLayout->addWidget(text2);
frameLayout->addWidget(btn);
frameLayout->addSpacerItem(spacer);    //设置弹簧
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值