就是想把lineEdit里面的值在slider里面显示,
就是在我自定义的槽函数里面调用了构造函数中的控件指针,结果显示的是野指针,请问我应该怎么改
部分代码如下:
//widget.h
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QSlider *slider;
QLineEdit *edit;
public slots:
void ChangeValue();
};
//widget.cpp 控件的布局什么的,还定义了一个定时器,定时触发槽函数
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QLineEdit *edit = new QLineEdit("25");
QSlider *slider = new QSlider(Qt::Vertical);
slider->setRange(0,100);
slider->setValue(0);
slider->setTickPosition(QSlider::TicksLeft);
QLabel *label = new QLabel("0");
QHBoxLayout *cLayout = new QHBoxLayout;
cLayout->addWidget(label,0,Qt::AlignRight);
cLayout->addWidget(slider,0,Qt::AlignLeft);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(edit);
mainLayout->addLayout(cLayout);
setLayout(mainLayout);
slider->setFocus();
QTimer *timer1 = new QTimer();
timer1->setInterval(500);
timer1->start();
connect(slider,SIGNAL(valueChanged(int)),label,SLOT(setNum(int)));
connect(timer1,SIGNAL(timeout()),this,SLOT(ChangeValue()));
}
Widget::~Widget(){}
void Widget::ChangeValue(){
QString s = edit->text();
int value = s.toInt();
slider->setValue(value);
}