QT-UI组件

布局

QGridLayout

网格布局器

  • void addWidget(QWidget *, int row, int column, Qt::Alignment = Qt::Alignment())
  • void setLayout(QLayout *) 父组件调用, 加入布局。

QVBoxLayout/QHBoxLayout

纵/横向网格布局器

  • setStretch(int index, int stretch)

组件大小控制

setMaximumSize(100,100)

QTableWidget

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ui->tableWidget->setColumnCount(columnNum);

patientMsgPage->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

QStringList InfHeader;
InfHeader<<tr("")<<tr("设备")<<tr("病人")<<tr("状态");
ui->tableWidget->setHorizontalHeaderLabels(InfHeader);

ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置为不可修改
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); //设置只能选择一行,不能多行选中
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//整行选择



添加信息

1
2
3
4
5
6
7
8
9
10
11
12
while(query->next())
{
//qDebug()<<query->value(1).toString();

patientMsgPage->setRowCount(i+1);

patientMsgPage->setItem(i,0,new QTableWidgetItem(query->value(0).toString()));
patientMsgPage->setItem(i,1,new QTableWidgetItem(query->value(1).toString()));
patientMsgPage->setItem(i,2,new QTableWidgetItem(query->value(2).toString()));
patientMsgPage->setItem(i,3,new QTableWidgetItem(query->value(3).toString()));
i++;
}

复选框

添加

1
2
3
QTableWidgetItem *checkBox = new QTableWidgetItem();
checkBox->setCheckState(Qt::Unchecked);
ui->tableWidget ->setItem(i, 0, checkBox);

检查状态

ui->tableWidget->item(row, col)->checkState() == Qt::Checked

事件

cellChanged

  • cellChanged(int row, int col) 获取改变的单元格的坐标

connect(ui->tableWidget, &QTableWidget::cellChanged, this, this->modifyList);

获取单元格信息

int headend = ui->tableWidget->model()->index(row, 1).data().toInt();

菜单栏

1
2
3
4
5
6
7
8
fileMenu =  ui->menuBar->addMenu("文件");
newFile = fileMenu->addAction("新建", this, slotNew, QKeySequence("Ctrl+N"));

toolBar = viewMenu->addAction("工具栏", this, slotToolBar);
toolBar->setCheckable(true);
toolBar->setChecked(true);

compile->setIcon(QIcon(":/icons/icons/build.png"));

QTabWidget

初始化

1
2
3
4
editorContainer = new QTabWidget();
editorContainer->setVisible(true);
editorContainer->clear();
editorContainer->setTabsClosable(true);

关闭请求

1
2
3
4
5
6
connect(editorContainer, SIGNAL(tabCloseRequested(int)), this, SLOT(onTabCloseRequested(int)));

void MainWindow::onTabCloseRequested(int index)
{
editorContainer->removeTab(index);
}

添加界面

1
2
int cur = editorContainer->addTab(editor, "untitled");
editorContainer->setCurrentIndex(cur);

获取当前界面

无界面时为-1, 下标从0开始。

1
2
int cur = editorContainer->currentIndex();
if(cur == -1) return ;

QMessageBox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
QMessageBox msgBox(QMessageBox::NoIcon,"警告","部分文件未保存,否仍然退出?", QMessageBox::Yes|QMessageBox::No, this);
int res = msgBox.exec();
if(res == QMessageBox::No) {
e->ignore();
}


QMessageBox::information(this, "提示", "尚未打开任何文件!");

QMessageBox msgBox(QMessageBox::NoIcon,"警告","文件未保存,是否保存修改?", QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel, this);
int res = msgBox.exec();
if(res == QMessageBox::Yes)
{
slotSave();
}
else if(res == QMessageBox::Cancel) {
return ;
}

常用函数

  • setWindowFlag(Qt::WindowStaysOnTopHint); 窗口置前
  • setWindowFlags(Qt::FramelessWindowHint) 去除边框
  • setWindowTitle("Hulu IDE");
  • setWindowIcon(QIcon(":/icons/icons/hulu.png"));

事件

关闭窗口请求

  • closeEvent(QCloseEvent * e)
1
2
3
4
5
6
7
8
9
10
void MainWindow::closeEvent(QCloseEvent * e)
{

QMessageBox msgBox(QMessageBox::NoIcon,"警告","部分文件未保存,否仍然退出?", QMessageBox::Yes|QMessageBox::No, this);
int res = msgBox.exec();
if(res == QMessageBox::No) {
e->ignore();
}

}

滚轮事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void MainWindow::wheelEvent(QWheelEvent *event)
{
QFont f = editor->font();
int size = f.pointSize();
if (QApplication::keyboardModifiers () == Qt::ControlModifier)
{
if(event->delta()>0) {
if(size<50) size++;
}
else {
if(size>7) size--;
}
f.setPointSize(size);
editor->setFont(f);
}
}