头文件:
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include<QDialog>
class QLabel;
class QLineEdit;
class QPushButton;
class GOTOCELLDIALOG:public QDialog
{
Q_OBJECT
public:
GOTOCELLDIALOG(QWidget *parent=0);
signals:
void stop();
private slots:
void enableokButton(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QPushButton *okButton;
QPushButton *cancelButton;
};
#endif // GOTOCELLDIALOG_H
实现文件:
#include<QtGui>
#include "gotocelldialog.h"
GOTOCELLDIALOG::GOTOCELLDIALOG(QWidget *parent)
:QDialog(parent)
{
label=new QLabel(tr("&Cell Location:"));
lineEdit=new QLineEdit;
label->setBuddy(lineEdit);
okButton=new QPushButton(tr("&OK"));
cancelButton=new QPushButton(tr("&Cancel"));
okButton->setDefault(true);
okButton->setEnabled(false);
connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableokButton(const QString &)));
connect(okButton,SIGNAL(clicked()),this,SLOT(close()));
connect(cancelButton,SIGNAL(clicked()),lineEdit,SLOT(clear()));
QHBoxLayout *topLayout=new QHBoxLayout;
topLayout->addWidget(label);
topLayout->addWidget(lineEdit);
QHBoxLayout *bottomLayout=new QHBoxLayout;
bottomLayout->addWidget(okButton);
bottomLayout->addWidget(cancelButton);
QVBoxLayout *mainlayout=new QVBoxLayout;
mainlayout->addLayout(topLayout);
mainlayout->addLayout(bottomLayout);
setLayout(mainlayout);
setWindowTitle(tr("Go To Cell"));
setFixedHeight(sizeHint().height());
}
void GOTOCELLDIALOG::enableokButton(const QString &text)
{
okButton->setEnabled(!lineEdit->text().isEmpty());
}
主函数:
#include <QtGui/QApplication>
#include "gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GOTOCELLDIALOG *dialog=new GOTOCELLDIALOG;
dialog->show();
return a.exec();
}
感想:通过今天的实践,我才深刻的体会到了什么叫做“能看得懂代码,却不会写代码”。看懂和会写之间还有很大的距离。尽管今天所实现的功能非常简单,而且也没有一次编译通过,但我觉得进步还是有的。对什么时候该用this指针有了进一步理解。同时我个人认为,手写代码更能锻炼一个人的能力,虽然来的更慢,而且效果不一定很好,但只要长期坚持,所能取得的进步就非常可观。