Qt中将lineEdit->text()赋给QString出错
#include<QtCore/QObject>#include<QtGui/QApplication>#include<QtGui/QHBoxLayout>#inclu...
#include <QtCore/QObject>
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include "dialog.h"
const static double PI = 3.1415926;
Dialog::Dialog(QWidget *parent)
:QDialog(parent)
{
QWidget *window = new QWidget;
window->setWindowTitle("Enter");
QLabel *label1 = new QLabel(QObject::tr("请输入圆的半径: "));
QLineEdit *lineEdit = new QLineEdit(this);
QLabel *label2 = new QLabel(this);
QPushButton *button = new QPushButton(QObject::tr("显示圆的面积: "));
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->addWidget(label1,0,0);
mainLayout->addWidget(lineEdit,0,1);
mainLayout->addWidget(label2,1,0);
mainLayout->addWidget(button,1,1);
QObject::connect(button,SIGNAL(clicked()),this,SLOT(showArea()));
window->setLayout(mainLayout);
window->show();
}
void Dialog::showArea()
{
bool ok;
QString tempStr;
QString valueStr = lineEdit->text();
int valueInt = valueStr.toInt(&ok);
double area = valueInt * valueInt * PI;
label2->setText(tempStr.setNum(area));
}
int main( int argc, char **argv )
{
QApplication qapp(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
Dialog w;
qapp.exec();
return 0;
}
程序我用断点跟踪了,信号槽可以触发,但是走到 QString valueStr = lineEdit->text();这一步时提示访问冲突,问题很奇怪,不知道该怎么解决。求大侠帮助! 展开
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include "dialog.h"
const static double PI = 3.1415926;
Dialog::Dialog(QWidget *parent)
:QDialog(parent)
{
QWidget *window = new QWidget;
window->setWindowTitle("Enter");
QLabel *label1 = new QLabel(QObject::tr("请输入圆的半径: "));
QLineEdit *lineEdit = new QLineEdit(this);
QLabel *label2 = new QLabel(this);
QPushButton *button = new QPushButton(QObject::tr("显示圆的面积: "));
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->addWidget(label1,0,0);
mainLayout->addWidget(lineEdit,0,1);
mainLayout->addWidget(label2,1,0);
mainLayout->addWidget(button,1,1);
QObject::connect(button,SIGNAL(clicked()),this,SLOT(showArea()));
window->setLayout(mainLayout);
window->show();
}
void Dialog::showArea()
{
bool ok;
QString tempStr;
QString valueStr = lineEdit->text();
int valueInt = valueStr.toInt(&ok);
double area = valueInt * valueInt * PI;
label2->setText(tempStr.setNum(area));
}
int main( int argc, char **argv )
{
QApplication qapp(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
Dialog w;
qapp.exec();
return 0;
}
程序我用断点跟踪了,信号槽可以触发,但是走到 QString valueStr = lineEdit->text();这一步时提示访问冲突,问题很奇怪,不知道该怎么解决。求大侠帮助! 展开
1个回答
展开全部
QLineEdit *lineEdit = new QLineEdit(this);
这个lineEdit 变量只在构造函数里有作用,你的void Dialog::showArea()
里访问的是一个没有初始化的变量。
我猜测"dialog.h"里有
QLineEdit *lineEdit
可是,你在构造函数里又来了一个 QLineEdit *lineEdit, 结果出了构造函数里,this->lineEdit还是野值。
建议:
class Dialog...
{
...
QLineEdit *lineEdit;
{
Dialog::Dialog(QWidget *parent)
:QDialog(parent)
{
QWidget *window = new QWidget;
window->setWindowTitle("Enter");
QLabel *label1 = new QLabel(QObject::tr("请输入圆的半径: "));
lineEdit = new QLineEdit(this);
这个lineEdit 变量只在构造函数里有作用,你的void Dialog::showArea()
里访问的是一个没有初始化的变量。
我猜测"dialog.h"里有
QLineEdit *lineEdit
可是,你在构造函数里又来了一个 QLineEdit *lineEdit, 结果出了构造函数里,this->lineEdit还是野值。
建议:
class Dialog...
{
...
QLineEdit *lineEdit;
{
Dialog::Dialog(QWidget *parent)
:QDialog(parent)
{
QWidget *window = new QWidget;
window->setWindowTitle("Enter");
QLabel *label1 = new QLabel(QObject::tr("请输入圆的半径: "));
lineEdit = new QLineEdit(this);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询