1. QRadioButton 类 对象名: radioButton_Back
1)不同于一般的控件清除clear,QRadioButton 的清除采用下列代码:
m_pUi->radioButton_Back->setCheckable(false);
m_pUi->radioButton_Back->setCheckable(true);
2)设定两个 radioButton 单次只能选中其中一个
用 Qt Designer打开Ui文件,在右侧属性编辑器内找到红框位置,勾选
!!!如果界面上有另外的控件也需要几选一,添加widget
如下图,四个checkBox 只能同时选择一个,选用选中autoExclusive的方法,但是如果图中还有两个radioButton 单次也只能选中其中一个,则需要新拖入containers中的widget控键,把两个radioButton 添加到widget控件内。这样每个widget可以独立执行单次只能选中其中一个的操作。
2. QGraphicsView 类 对象名:graphicsView
显示图片及清除操作:
QGraphicsScene* m_pScene = new QGraphicsScene;
m_pScene->addPixmap(QPixmap("C:\\Users\\.....图标.jpg"));
m_pUi->graphicsView->setScene(m_pScene);
m_pUi->graphicsView->show(); //显示图像
m_pScene->clear(); //清除图像
// 清空创建的指针,防内存泄漏(添加到析构函数内)
if (m_pScene)
{
delete m_pScene;
m_pScene = nullptr;
}
3. QTableView 类 对象名:tableViewJob
tableview 类 与 tableModel 类 合用,可用于生成 界面表格 无需手动画
QueueTableModel* pTableModel;//QueueTableModel为一个自定义的类
// 一般会重写基类的几个接口
class QueueTableModel : public QAbstractTableModel
{
public:
QueueTableModel(QObject* pParent = nullptr);
virtual ~QueueTableModel();
public:
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; // 设置表格的表头
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;//设置表格的各项表头的数据来源
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
}
m_pUi->tableViewJob->setModel(pTableModel); //设置表格的model,显示在view内
m_pUi->tableViewJob->setSelectionBehavior(QAbstractItemView::SelectRows); //设置鼠标点击选中的为一整行
m_pUi->tableViewGroup->setSelectionMode(QAbstractItemView::SingleSelection); // 与上一句合用可设置只允许选中单行
m_pUi->tableViewGroup->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置表格不可编辑
m_pUi->tableViewGroup->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);//使第一列的内容完全显示
m_pUi->tableViewJob->setContextMenuPolicy(Qt::CustomContextMenu); //可弹出右键菜单
QModelIndex indexlist = m_pUi->tableViewJob->selectionModel()->selectedRows(); // 获取当前选中的多行
QModelIndex index = m_pUi->tableViewGroup->currentIndex(); //获取当前选中行