以下是一个简单的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"