QFileInfo及QWidget构建复合按钮

// 文件最近一次修改时间
QFileInfo fi(fileName);
qDebug()<

// 除后缀名之外的全部字符串集
fi.completeBaseName()

// 到第一个 . (点) 的字符串集
fi.baseName()

如文件名为 123.1.0.txt,前者结果为:123.1.0
后者结果为:123

完整代码:

///!
//! \brief CStartPage::createRecentDocs 构建右侧的 “最近文件” 列表
//!
void CStartPage::createRecentDocs()
{
    const auto &recentList = m_parent->getRecentFilesList();
    int i = 0;

    ui.CleanRecentButton->setVisible(recentList.size());

    for (const QString &fileName : recentList)
    {
        QWidget *host = new QWidget(this);
        host->setLayout(new QHBoxLayout);

        QFileInfo fi(fileName);
        QToolButton *deleteButton = new QToolButton(host);
        deleteButton->setAutoRaise(true);
        deleteButton->setIcon(QIcon(":/Icons/Delete"));
        deleteButton->setToolTip(tr("Remove this file from the list"));

        QAction *deleteAction = new QAction(fileName, deleteButton);
        deleteAction->setData(i);// 以当前行序号作为Data索引

        connect(deleteButton, &QCommandLinkButton::clicked, deleteAction, &QAction::triggered);
        connect(deleteAction, &QAction::triggered, this, &CStartPage::onRemoveDocument);

        qDebug()<setMinimumHeight(64);

        QAction *recentAction = new QAction(fileName, fileButton);
        recentAction->setData(i);

        connect(fileButton, &QCommandLinkButton::clicked, recentAction, &QAction::triggered);
        connect(recentAction, &QAction::triggered, this, &CStartPage::onRecentDocument);

        host->layout()->addWidget(fileButton);
        host->layout()->addWidget(deleteButton);

        ui.RightWidget->layout()->addWidget(host);
        m_recentFileBtns[i++] = host;
    }


    // spacer 使列表对象“挤”在上方
    QWidget *temp = new QWidget(this);
    temp->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
    ui.RightWidget->layout()->addWidget(temp);
}

以上代码很好的展示了如何构建QWidget及layout,并用纯代码方式搭建同时多个元素的复合按钮对象
最后一段代码通过构建一个新的QWidget去占位,实现类似 Creator设计师 中添加弹簧的效果

你可能感兴趣的:(QFileInfo及QWidget构建复合按钮)