报内存不能为written问题,请大侠指点小菜鸟。。
头文件
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include <QtGui/QWidget>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
class addressbook : public QWidget
{
Q_OBJECT
public:
addressbook(QWidget *parent = 0);
~addressbook();
private:
QLineEdit*nameLine;
QTextEdit*addressText;
QPushButton*addButton;
QPushButton*submitButton;
QPushButton*cancelButton;
QVBoxLayout*buttonLayout1;
QMap<QString,QString>contacts;
//QMap对象contacts带有一个键值对:联系人作为键,而联系人地址作为值
//还会声明两个私有QString对象:
QString oldName;
QString oldAddress;
public slots:
void addContact();
void submitContact();
void cancel();
};
#endif // ADDRESSBOOK_H
--------------------------------
addressbook.cpp
#include "addressbook.h"
#include <QLabel>
//#include <QGridLayout>
//#include <QVBoxLayout>
//#include <QString>
#include <QMessageBox>
addressbook::addressbook(QWidget *parent)
: QWidget(parent)
{
QLabel*nameLabel = new QLabel(tr("Name:"));//命名为name的标签(Label)
nameLine = new QLineEdit;
QLabel*addressLabel =new QLabel(tr("Address:"));//命名为Address的标签(Label)
addressText = new QTextEdit;
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);
//0mainLayout->addLayout(buttonLayout1,1,2);//这句出问题了?
// ?加上这段就出现0x6a2639c4指令引用0x5388e84内存.该内存不能写的问题
//QGridLayout*mainLayout = new QGridLayout;//创建对象mainLayout
//mainLayout->addWidget(nameLabel,0,0);
//mainLayout->addWidget(nameLine,0,1);
//mainLayout->addWidget(addressLabel,1,0,Qt::AlignTop);//确保addressLabel不会被放在坐标(1,0)的正中央
//mainLayout->addWidget(addressText,1,1);
setLayout(mainLayout);
setWindowTitle(tr("Simple Address Book"));//setLayout()是widget的一个设置标题的函数
nameLine->setReadOnly(true);//只读
addressText->setReadOnly(true);//只读
//右按钮区域---------------------------------------
addButton = new QPushButton(tr("&Add"));
addButton->show();
submitButton = new QPushButton(tr("&Submit"));
submitButton->hide();
cancelButton = new QPushButton(tr("&Cancel"));
cancelButton->hide();
//显示通过调用show(),隐藏通过调用hide(),这两个操作只有在用户点击贴加时才会显示,
//而操作是通过在中文说明的addContact()处理的
QVBoxLayout*buttonLayout1 = new QVBoxLayout;
buttonLayout1->addWidget(addButton,Qt::AlignTop);
buttonLayout1->addWidget(submitButton);
buttonLayout1->addWidget(cancelButton);
buttonLayout1->addStretch();
//addStretch()确保按钮并不是采用均匀间隔排列的,而是更靠近widget的顶部。
connect(addButton,SIGNAL(clicked()),this,SLOT(addContact()));
connect(submitButton,SIGNAL(clicked()),this,SLOT(submitContact()));
connect(cancelButton,SIGNAL(clicked()),this,SLOT(cancel()));
//this表示addressBook
}
addressbook::~addressbook()
{
}
-------------------------------------
mian.cpp
#include <QtGui/QApplication>
#include "addressbook.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
addressbook w;
w.show();
return a.exec();
}
[解决办法]
QVBoxLayout*buttonLayout1 = new QVBoxLayout;
还在后面才创建,前面就用上了