Skip to content

Instantly share code, notes, and snippets.

@yuwen41200
Created June 14, 2015 20:26
Show Gist options
  • Save yuwen41200/970691a7b7464a588967 to your computer and use it in GitHub Desktop.
Save yuwen41200/970691a7b7464a588967 to your computer and use it in GitHub Desktop.
Simple Qt Test 2
#include <QtGui>
#include "dialog.h"
Dialog::Dialog(QWidget *parent) : QDialog(parent) {
label = new QLabel(tr("Find &what"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Case &sensitive"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("&Close"));
connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const QString &)));
connect(findButton, SIGNAL(clicked()), this, SLOT(clickFindButton()));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setWindowTitle("Searching Dialog");
setLayout(mainLayout);
}
void Dialog::clickFindButton() {
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
if (backwardCheckBox->isChecked()) emit findPrevious(text, cs);
else emit findNext(text, cs);
}
void Dialog::enableFindButton(const QString &text) {
findButton->setEnabled(!text.isEmpty());
}
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
#include <QHBoxLayout>
class Dialog : public QDialog {
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots:
void clickFindButton();
void enableFindButton(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif
#include <QApplication>
#include "dialog.h"
int main(int argc, char *argv[]) {
QApplication application(argc, argv);
Dialog dialog;
dialog.show();
return application.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment