[c++]代码库
//设置ListWidget
//contentsWidget = new QListWidget; //根据ui设置来
contentsWidget->setViewMode(QListView::IconMode);
contentsWidget->setIconSize(QSize(96, 84));
contentsWidget->setMovement(QListView::Static);
contentsWidget->setMaximumWidth(128);
contentsWidget->setSpacing(12);
//添加子项,注意声明的是QListWidgetItem变量
QListWidgetItem *configBtn = new QListWidgetItem(ui->contentsWidget);
configBtn->setIcon(QIcon(":/images/config.png"));
configBtn->setText(tr("Configuration"));
configBtn->setTextAlignment(Qt::AlignHCenter);
configBtn->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
...
//连接信号
connect(ui->contentsWidget,
SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this,
SLOT(changePage(QListWidgetItem*, QListWidgetItem*))
);
//修改当前stackedWidget的代码片段:
void ConfigDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
{
if (!current)
current = previous;
pagesWidget->setCurrentIndex(contentsWidget->row(current));
}