QT开发中文网

yQt主界面由新建、删除、编辑和搜索按钮组成,这些按钮的功能都是操作TabWidget。具体功能介绍如下: 新建按钮:点击此按钮将弹出一个对话框, 对话框中有 输入名称 描述 新建时间 修改时间,输入完点击确定,将信息插入到 tabwidget 删除按钮:点击此按钮将删除当前选中的一条数据,并将其从TabWidget中移除。 编辑按钮:点击此按钮将打开当前选中的一条数据,允许用户对其进行编辑。 搜索按钮:点击此按钮将弹出一个搜索框,用户可以输入关键词来搜索表中 所有数据的...

以下是一个简单的Qt主界面代码示例,其中包含新建、删除、编辑和搜索按钮的功能实现:

#include <QtWidgets>

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    MainWindow(QWidget *parent = nullptr);
    
private slots:
    void onNewClicked();
    void onDeleteClicked();
    void onEditClicked();
    void onSearchClicked();
    
private:
    QTabWidget *m_tabWidget;
    QPushButton *m_newButton;
    QPushButton *m_deleteButton;
    QPushButton *m_editButton;
    QPushButton *m_searchButton;
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // Create the tab widget
    m_tabWidget = new QTabWidget(this);
    setCentralWidget(m_tabWidget);
    
    // Create the buttons
    m_newButton = new QPushButton(tr("New"), this);
    connect(m_newButton, &QPushButton::clicked, this, &MainWindow::onNewClicked);
    
    m_deleteButton = new QPushButton(tr("Delete"), this);
    connect(m_deleteButton, &QPushButton::clicked, this, &MainWindow::onDeleteClicked);
    
    m_editButton = new QPushButton(tr("Edit"), this);
    connect(m_editButton, &QPushButton::clicked, this, &MainWindow::onEditClicked);
    
    m_searchButton = new QPushButton(tr("Search"), this);
    connect(m_searchButton, &QPushButton::clicked, this, &MainWindow::onSearchClicked);
    
    // Add the buttons to the toolbar
    QToolBar *toolbar = new QToolBar(this);
    toolbar->addWidget(m_newButton);
    toolbar->addWidget(m_deleteButton);
    toolbar->addWidget(m_editButton);
    toolbar->addWidget(m_searchButton);
    addToolBar(toolbar);
}

void MainWindow::onNewClicked()
{
    // Prompt the user for input and create a new tab
    QDialog dialog(this);
    QFormLayout layout(&dialog);
    QLineEdit *nameEdit = new QLineEdit(&dialog);
    QLineEdit *descEdit = new QLineEdit(&dialog);
    QDateTimeEdit *createTimeEdit = new QDateTimeEdit(QDateTime::currentDateTime(), &dialog);
    QDateTimeEdit *modifyTimeEdit = new QDateTimeEdit(QDateTime::currentDateTime(), &dialog);
    layout.addRow(tr("Name:"), nameEdit);
    layout.addRow(tr("Description:"), descEdit);
    layout.addRow(tr("Create time:"), createTimeEdit);
    layout.addRow(tr("Modify time:"), modifyTimeEdit);
    QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
    layout.addRow(&buttonBox);
    connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
    connect(&buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
    if (dialog.exec() == QDialog::Accepted) {
        QString name = nameEdit->text();
        QString desc = descEdit->text();
        QDateTime createTime = createTimeEdit->dateTime();
        QDateTime modifyTime = modifyTimeEdit->dateTime();
        QWidget *tab = new QWidget(m_tabWidget);
        QVBoxLayout *tabLayout = new QVBoxLayout(tab);
        QLabel *nameLabel = new QLabel(name, tab);
        QLabel *descLabel = new QLabel(desc, tab);
        QLabel *createTimeLabel = new QLabel(createTime.toString(Qt::ISODate), tab);
        QLabel *modifyTimeLabel = new QLabel(modifyTime.toString(Qt::ISODate), tab);
        tabLayout->addWidget(nameLabel);
        tabLayout->addWidget(descLabel);
        tabLayout->addWidget(createTimeLabel);
        tabLayout->addWidget(modifyTimeLabel);
        m_tabWidget->addTab(tab, name);
    }
}

void MainWindow::onDeleteClicked()
{
    // Remove the current tab
    int index = m_tabWidget->currentIndex();
    if (index >= 0) {
        QWidget *tab = m_tabWidget->widget(index);
        delete tab;
    }
}

void MainWindow::onEditClicked()
{
    // Edit the current tab
    int index = m_tabWidget->currentIndex();
    if (index >= 0) {
        QWidget *tab = m_tabWidget->widget(index);
        QDialog dialog(this);
        QVBoxLayout layout(&dialog);
        QLabel *nameLabel = new QLabel(tr("Name:"), &dialog);
        QLineEdit *nameEdit = new QLineEdit(tab);
        QLabel *descLabel = new QLabel(tr("Description:"), &dialog);
        QLineEdit *descEdit = new QLineEdit(tab);
        QPushButton *okButton = new QPushButton(tr("OK"), &dialog);
        connect(okButton, &QPushButton::clicked, &dialog, &QDialog::accept);
        layout.addWidget(nameLabel);
        layout.addWidget(nameEdit);
        layout.addWidget(descLabel);
        layout.addWidget(descEdit);
        layout.addWidget(okButton);
        dialog.exec();
    }
}

void MainWindow::onSearchClicked()
{
    // Search for a keyword in the tab content and highlight the results
    QString keyword = QInputDialog::getText(this, tr("Search"), tr("Enter keyword:"));
    if (!keyword.isEmpty()) {
        QTextDocument *doc = m_tabWidget->currentWidget()->findChild<QTextDocument *>();
        if (doc) {
            QTextCursor cursor(doc);
            QTextCharFormat format;
            format.setBackground(QBrush(Qt::yellow));
            while (!cursor.isNull() && !cursor.atEnd()) {
                cursor = doc->find(keyword, cursor, QTextDocument::FindWholeWords);
                if (!cursor.isNull()) {
                    cursor.mergeCharFormat(format);
                }
            }
        }
    }
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
}

#include "main.moc"

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=1090

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?