注册 登录
编程论坛 QT论坛

不知道有没有人在,想请教一个关于QT代码的问题

c453413516 发布于 2011-08-24 12:18, 849 次点击
问题描述:这是书上的部分关键部分代码,我自己添加了一些重要的部分后,试着运行,编译通过,最后生成EXE文件,试着实际运行一下,结果:原本是按“add”键可以在两个输入框内输入文字,并且还有两个隐藏按键会显示出来,但是结果没有达到预期的效果,能否帮我检查一下到底是除了什么问题?谢谢了
以下是源代码:
头文件:
#ifndef MYNAME_H_
#define MYNAME_H_

#include <QWidget>
#include <QMap>

class QLineEdit;
class QTextEdit;
class QPushButton;
class QString;

class AddressBook : public QWidget
{
    Q_OBJECT
   
public:
    AddressBook(QWidget *parent = 0);
private slots:
    void addContact();
    void submitContact();
    void cancel();
private:
    QLineEdit *nameLine;
    QTextEdit *addressText;
    QPushButton *addButton;
    QPushButton *submitButton;
    QPushButton *cancelButton;
    QMap <QString,QString> contacts;
    QString oldName;
    QString oldAddress;
};

#endif

类的实现文件:
#include <QtGui>

#include "AddressBook.h"

AddressBook :: AddressBook(QWidget *parent):QWidget(parent)
{
    QLabel *nameLabel = new QLabel(tr("Name:"));
    nameLine = new QLineEdit;
    nameLine->setReadOnly(true);
    QLabel *addressLabel = new QLabel(tr("Address: "));
    addressText = new QTextEdit;
    addressText->setReadOnly(true);
    addButton = new QPushButton(tr("&Add"));
    addButton->show();
    submitButton = new QPushButton(tr("&Submit"));
    submitButton->hide();
    cancelButton = new QPushButton(tr("&Cancel"));
    cancelButton->hide();

    connect(addButton, SIGNAL(cliked()), this, SLOT(addContact()));
    connect(submitButton, SIGNAL(cliked()), this, SLOT(submitContact()));
    connect(cancelButton, SIGNAL(cliked()), this, SLOT(cancel()));

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(nameLabel,0,0);
    mainLayout->addWidget(nameLine,0,1);
    mainLayout->addWidget(addressLabel,1,0,Qt::AlignTop);
    mainLayout->addWidget(addressText,1,1);
    QVBoxLayout *buttonLayout1 = new QVBoxLayout;
    buttonLayout1->addWidget(addButton,Qt::AlignTop);
    buttonLayout1->addWidget(submitButton);
    buttonLayout1->addWidget(cancelButton);
    buttonLayout1->addStretch();
    mainLayout->addLayout(buttonLayout1,1,2);
   
    setLayout(mainLayout);
    setWindowTitle(tr("Simple Address Book"));
}

void AddressBook::addContact()
{
    oldName = nameLine->text();
    oldAddress = addressText->toPlainText();

    nameLine->clear();
    addressText->clear();

    nameLine->setReadOnly(false);
    nameLine->setFocus(Qt::OtherFocusReason);
    addressText->setReadOnly(false);

    addButton->setEnabled(false);
    submitButton->show();
    cancelButton->show();
}

void AddressBook::submitContact()
{
    QString name = nameLine->text();
    QString address = addressText->toPlainText();

    if (name == " "||address == " ")
    {
        QMessageBox::information(this, tr("Empty Field"), tr("Please enter a name and address."));
        return;
    }
   
    if(!contacts.contains(name))
    {
        contacts.insert(name,address);
        QMessageBox::information(this, tr("Add Successful"), tr("\"%1\"has been added to your address book.").arg(name));
    }
   
   
    if (contacts.isEmpty())
    {
        nameLine->clear();
        addressText->clear();
    }

    nameLine->setReadOnly(true);
    addressText->setReadOnly(true);
    addButton->setEnabled(true);
    submitButton->hide();
    cancelButton->hide();
}

void AddressBook::cancel()
{
    nameLine->setText(oldName);
    nameLine->setReadOnly(true);

    addressText->setText(oldAddress);
    addressText->setReadOnly(true);

    addButton->setEnabled(true);
    submitButton->hide();
    cancelButton->hide();
}



main.cpp文件(个人觉得这个文件应该没有问题):
#include <QApplication>

#include "AddressBook.h"

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
   
    AddressBook *addressBook = new AddressBook;
    addressBook->show();

    return app.exec();   
}
2 回复
#2
myseemylife2011-12-13 17:02
。。。。好多代码。。。。
button你用setVisible吧。。虽然show,hide也是调用visible、但是看着好别扭。
 AddressBook *addressBook = new AddressBook;
    addressBook->show();

有内存泄漏。、、、、、、给个this去构造。。。
其他的看不来了。。。
#3
星魂流云2011-12-14 11:50
1