Qt编程指南 ■ QWidget ■ QTabWidget ■ QTableWidget ■ QListWidget ■ QStackedWidget ■ QCalendarWidget ■
■ QWidget
vWidget[ i] = new QWidget ( ) ;
vWidget[ i] -> setAutoFillBackground ( true ) ;
该函数用于设置当窗口作为被包含窗口时, 是否需要绘制背景。
注意:QT窗口系统默认状态下, 当一个窗口作为被包含窗口时, 其不再绘制背景, 即使指定了背景绘制参数( 如背景颜色、背景图片等) 。```c
QWidget{
background- color: rgb ( 255 , 170 , 127 ) ;
}
ui-> widget-> setStyleSheet ( QString :: fromUtf8 ( "border:3px solid blue" ) ) ;
ui-> widget-> setStyleSheet ( QString :: fromUtf8 ( "#widget_2{border:3px solid red}" ) ) ; background- color: rgb ( 52 , 57 , 71 ) ;
border- bottom- left- radius: 20 px;
■ QTabWidget
MainWindow :: MainWindow ( QWidget * parent) : QMainWindow ( parent)
{ this -> setGeometry ( 0 , 0 , 800 , 480 ) ; widget = new QWidget ( this ) ; this -> setCentralWidget ( widget) ; tabWidget = new QTabWidget ( ) ; hBoxLayout = new QHBoxLayout ( ) ; QList < QString> strLabelList; strLabelList<< "标签一" << "标签二" << "标签三" ; QList < QString> strTabList; strTabList<< "页面一" << "页面二" << "页面三" ; QList < QString> iconList; iconList<< ":/icons/icon1" << ":/icons/icon2" << ":/icons/icon3" ; for ( int i = 0 ; i < 3 ; i++ ) { label[ i] = new QLabel ( ) ; label[ i] -> setText ( strLabelList[ i] ) ; label[ i] -> setAlignment ( Qt:: AlignCenter) ; tabWidget-> addTab ( label[ i] , QIcon ( iconList[ i] ) , strTabList[ i] ) ; } hBoxLayout-> addWidget ( tabWidget) ; widget-> setLayout ( hBoxLayout) ;
}
MainWindow :: ~ MainWindow ( )
{
}
■ QTableWidget
MainWindow :: MainWindow ( QWidget * parent) : QMainWindow ( parent)
{ this -> setGeometry ( 0 , 0 , 800 , 480 ) ; tableWidget = new QTableWidget ( this ) ; setCentralWidget ( tableWidget) ; tableWidget-> setColumnCount ( 2 ) ; tableWidget-> setRowCount ( 2 ) ; tableWidget-> setHorizontalHeaderLabels ( QStringList ( ) << "姓名" << "性别" ) ; QList < QString> strList; strList<< "小明" << "小红" << "男" << "女" ; for ( int i = 0 ; i < 4 ; i++ ) { tableWidgetItem[ i] = new QTableWidgetItem ( strList[ i] ) ; tableWidgetItem[ i] -> setTextAlignment ( Qt:: AlignCenter) ; } tableWidget-> setItem ( 0 , 0 , tableWidgetItem[ 0 ] ) ; tableWidget-> setItem ( 1 , 0 , tableWidgetItem[ 1 ] ) ; tableWidget-> setItem ( 0 , 1 , tableWidgetItem[ 2 ] ) ; tableWidget-> setItem ( 1 , 1 , tableWidgetItem[ 3 ] ) ;
}
MainWindow :: ~ MainWindow ( )
{
}
■ QListWidget
void ProManageEdit :: add ( QStringList stlist)
{ QListWidgetItem * item0 = new QListWidgetItem; ListManagEdit * editWidget = new ListManagEdit ( this ) ; editWidget-> update ( stlist) ; connect ( editWidget, SIGNAL ( edit_textChanged ( int , QStringList) ) , this , SLOT ( slots_edit_textChangedHandle ( int , QStringList) ) ) ; ui-> listWidget-> addItem ( item0) ; ui-> listWidget-> setItemWidget ( item0, editWidget) ;
}
void ProManageEdit :: deleteStep ( int step)
{ auto editWidget = dynamic_cast < ListManagEdit* > ( ui-> listWidget-> itemWidget ( ui-> listWidget-> item ( step) ) ) ; disconnect ( editWidget, SIGNAL ( edit_textChanged ( int , QStringList) ) , this , SLOT ( slots_edit_textChangedHandle ( int , QStringList) ) ) ; ui-> listWidget-> removeItemWidget ( ui-> listWidget-> item ( step) ) ;
}
void ProManageEdit :: swap ( int fromstep, QStringList fromstlist, int tostep, QStringList tostlist)
{ QListWidgetItem* fromitem = ui-> listWidget-> item ( fromstep) ; QListWidgetItem* toitem = ui-> listWidget-> item ( tostep) ; auto fromTest = dynamic_cast < ListManagEdit* > ( ui-> listWidget-> itemWidget ( fromitem) ) ; auto toTest = dynamic_cast < ListManagEdit* > ( ui-> listWidget-> itemWidget ( toitem) ) ; fromTest-> update ( tostlist) ; toTest-> update ( fromstlist) ;
}
listWidget-> item ( i) -> setTextColor ( QColor ( 22 , 22 , 22 , 225 ) ) ;
ui-> listWidget-> setSpacing ( 5 ) ;
设置每个QListWidgetItem 之间的距离
QListWidget 对不齐问题,是因为进度条导致的解决办法是/ 屏蔽垂直滚动条
ui-> listWidget-> setVerticalScrollBarPolicy ( Qt:: ScrollBarAlwaysOff) ;
■ QStackedWidget
MainWindow :: MainWindow ( QWidget * parent) : QMainWindow ( parent)
{ this -> setGeometry ( 0 , 0 , 800 , 480 ) ; widget = new QWidget ( this ) ; this -> setCentralWidget ( widget) ; hBoxLayout = new QHBoxLayout ( ) ; stackedWidget = new QStackedWidget ( ) ; listWidget = new QListWidget ( ) ; QList < QString> strListWidgetList; strListWidgetList<< "窗口一" << "窗口二" << "窗口三" ; for ( int i = 0 ; i < 3 ; i++ ) { listWidget-> insertItem ( i, strListWidgetList[ i] ) ; } QList < QString> strLabelList; strLabelList<< "标签一" << "标签二" << "标签三" ; for ( int i = 0 ; i < 3 ; i++ ) { label[ i] = new QLabel ( ) ; label[ i] -> setText ( strLabelList[ i] ) ; label[ i] -> setAlignment ( Qt:: AlignCenter) ; stackedWidget-> addWidget ( label[ i] ) ; } listWidget-> setMaximumWidth ( 200 ) ; hBoxLayout-> addWidget ( listWidget) ; hBoxLayout-> addWidget ( stackedWidget) ; widget-> setLayout ( hBoxLayout) ; connect ( listWidget, SIGNAL ( currentRowChanged ( int ) ) , stackedWidget, SLOT ( setCurrentIndex ( int ) ) ) ;
}
MainWindow :: ~ MainWindow ( )
{
}
■ QCalendarWidget
calendarWidget = new QCalendarWidget ( this ) ; calendarWidget-> setGeometry ( 200 , 20 , 400 , 300 ) ; QFont font; font. setPixelSize ( 10 ) ; calendarWidget-> setFont ( font) ; pushButton = new QPushButton ( "回到当前日期" , this ) ; pushButton-> setGeometry ( 200 , 350 , 100 , 30 ) ; label = new QLabel ( this ) ; label-> setGeometry ( 400 , 350 , 400 , 30 ) ; QString str = "当前选择的日期:" + calendarWidget-> selectedDate ( ) . toString ( ) ; label-> setText ( str) ; connect ( calendarWidget, SIGNAL ( selectionChanged ( ) ) , this , SLOT ( calendarWidgetSelectionChanged ( ) ) ) ; connect ( pushButton, SIGNAL ( clicked ( ) ) , this , SLOT ( pushButtonClicked ( ) ) ) ; MainWindow :: ~ MainWindow ( )
{ }
void MainWindow :: calendarWidgetSelectionChanged ( )
{ QString str = "当前选择的日期:" + calendarWidget-> selectedDate ( ) . toString ( ) ; label-> setText ( str) ;
} void MainWindow :: pushButtonClicked ( )
{ calendarWidget-> setSelectedDate ( QDate :: currentDate ( ) ) ;
}
■