//
//表格表头的显示与隐藏
tableWidget->verticalHeader()->setVisible(false) //隐藏列表头
tableWidget->horizontalHeader()->setVisible(false)//隐藏行表头
//tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers)
//设置行高
for(int i= 0i<tableWidget->rowCount()i++)
{
tableWidget->setRowHeight(i, 60)
}
//设置列宽
for(int i= 0i<tableWidget->columnCount()i++)
{
tableWidget->setColumnWidth(i, 185)
}
// 单元格中添加控件并居中
QLabel* label = new QLabel("gender")
QComboBox *comBox = new QComboBox()
comBox->setFixedSize(100, 25)
comBox->addItem("F")
comBox->addItem("M")
// 单元格中的控件需要通过布局管理
QWidget *widget = new QWidget
QHBoxLayout *hLayout
hLayout = new QHBoxLayout()
hLayout->addWidget( label)
hLayout->addWidget(comBox)
hLayout->setMargin(0)
hLayout->setAlignment(widget, Qt::AlignCenter)
hLayout->setContentsMargins(10, 0, 20, 0)
widget->setLayout(hLayout)
// 添加单元格
tableWidget->setCellWidget(0,0,widget)
QHBoxLayout* mainLayout = new QHBoxLayout
mainLayout->addWidget( tableWidget)
setLayout(mainLayout)
QVector<QComboBox*> cbDianYafor(int i=0i<20i++)
{
QComboBox *tmp= new QComboBox()
tmp->addItem("")
tmp->addItem("100")
tmp->addItem("150")
tmp->addItem("300")
cbDianYa.append(tmp)
}
for(int j=0j<20j++)
{
ui->tableWidget->setCellWidget(j,0,cbDianYa.at(j))
}
这回可行,我试过了,用个向量解决问题
一、Qt的半自动化的内存管理
(1)QObject及其派生类的对象,如果其parent非0,那么其parent析构时会析构该对象。
(2)QWidget及其派生类的对象,可以设置 Qt::WA_DeleteOnClose 标志位(当close时会析构该对象)。
(3)QAbstractAnimation派生类的对象,可以设置 QAbstractAnimation::DeleteWhenStopped。
(4)QRunnable::setAutoDelete()、MediaSource::setAutoDelete()。
(5)父子关系:父对象、子对象、父子关系。这是Qt中所特有的,与类的继承关系无关,传递参数是与parent有关(基类、派生类,或父类、子类,这是对于派生体系来说的,与parent无关)。
//参考 https://blog.csdn.net/qq_42100881/article/details/80447441
二、布局、tabwiget 、tablewidget 、tableView treeview treeWidget添加控件需要自己手动释放不
1、布局
mywidget* pp = new mywidget
ui->verticalLayout->addWidget(pp)//verticalLayout 为主窗口 ui中的布局
测试:关闭主窗口, mywidget 的析构函数会自动进入
2、tabwiget
mywidget* pTest = new mywidget//其中没有设置setAttribute(Qt::WA_DeleteOnClose)
ui->tabWidget->addTab(pTest, "test")
测试:关闭主窗口, mywidget 的析构函数会自动进入
3、tablewidget
ui->tableWidget->setRowCount(2)//设置行数为2
ui->tableWidget->setColumnCount(2)//设置列数为5
myQComboBox* combox = new myQComboBox//myQComboBox自己继承QComboBox的 没有指定父亲,
ui->tableWidget->setCellWidget(0,0,combox)
测试:关闭主窗口, myQComboBox 的析构函数会自动进入
4、tableView
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)