快捷搜索:  汽车  科技

qt 在表格中添加控件(qt的表格里放控件)

qt 在表格中添加控件(qt的表格里放控件)

1、背景

就是表格里放控件,或者说是动态生成控件,就像Excel一样,这样操作方便,这也是界面强大的体现,以前的界面都是一些文本框之类的,显然操作比较简单,如果复杂逻辑的话,处理就比较麻烦了

2、实例2.1、界面情况:

qt 在表格中添加控件(qt的表格里放控件)(1)

2.2、头文件的情况

#ifndef DYNAMICCTRLDIALOG_H #define DYNAMICCTRLDIALOG_H #include <QDialog> #include <QTableWidget> namespace Ui { class DynamicCTRLDialog; } class DynamicCTRLDialog : public QDialog { Q_OBJECT public: explicit DynamicCTRLDialog(QWidget *parent = 0); ~DynamicCTRLDialog(); void init(); private slots: void changTextListPath(QString str); private: Ui::DynamicCTRLDialog *ui; QTableWidget *m_tableWidget; }; #endif // DYNAMICCTRLDIALOG_H 2.2、在父窗口上生成控件

m_tableWidget = ui->tableWidget; m_tableWidget->setRowCount(1); QTableWidgetItem *item = new QTableWidgetItem(); m_tableWidget->setItem(0 0 item); item->setText(QString::fromStdString("小明")); item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter); item = new QTableWidgetItem(); m_tableWidget->setItem(0 1 item); //1/new一个控件,并且指定其父窗口 QComboBox *combobox_Args=new QComboBox(m_tableWidget); QStringList combobox_subArgs_list; combobox_subArgs_list.append("1班"); combobox_subArgs_list.append("2班"); combobox_Args->addItems(combobox_subArgs_list); combobox_Args->setCurrentIndex(1); 2.2 、指定表格的位置

//2/指定位置 m_tableWidget->setCellWidget(0 1 combobox_Args);2.3、指定控件的事件

connect(combobox_Args SIGNAL(currentIndexChanged(QString)) this SLOT(changTextListPath(QString))); void DynamicCTRLDialog::changTextListPath(QString str) { qDebug()<<str; }2.4、效果情况

qt 在表格中添加控件(qt的表格里放控件)(2)

3、总结

综合来说实现挺简单的,关键一步就是在父窗口上生成控件,在父窗口生成控件,体现到界面上,就是,控件成为了父窗口的一部分,然后,再放到指定位置就可以了。

**创建控件时,可以指定停靠在某个父窗口上面,这时控件将作为子窗口被束缚在其父窗口的内部,

并伴随父窗口一起移动,隐藏,显示和关闭;否则,该控件将作为独立窗口显示在屏幕上,且游离于其它窗口之外**

qt 在表格中添加控件(qt的表格里放控件)(3)

猜您喜欢: