<急>Qt:为什么我这个函数一执行就有段错误
/************************dialog.h*********************/
#include <QApplication>
#include <QPushButton>
#include<QWidget>
#include<QtGui>
class Dialogs : public QWidget
{
Q_OBJECT
public:
Dialogs();
public:
QFrame *frame;
QPushButton * button0;
QPushButton * button1 ;
QPushButton * button2 ;
private slots:
void slotbutton0();
void slotbutton1();
// void slotbutton2();
};
/************************dialog.cpp*********************/
#include <QApplication>
#include<stdio.h>
#include<unistd.h>
#include <QtGui>
#include"dialog.h"
int pid;
Dialogs::Dialogs( )
{
frame->setObjectName("myframe");
frame->resize(320,240);
frame->setStyleSheet("QFrame#myframe{border-image:url(images/background.png)}" );
button0->setGeometry(260,30,50,50);
button1->setGeometry(260,90,50,50);
button2->setGeometry(260,150,50,50);
QIcon icon;
QPixmap pixmap0("images/Start_amended.png");
icon.addPixmap(pixmap0);
button0->setIcon(icon);
button0->setIconSize(QSize(68,68));
button0->setFixedSize(pixmap0.size());
button0->setMask(pixmap0.mask());
QPixmap pixmap1("images/Stop_amended.png");
icon.addPixmap(pixmap1);
button1->setIcon(icon);
button1->setIconSize(QSize(50,50));
button1->setFixedSize(pixmap1.size());
button1->setMask(pixmap1.mask());
QPixmap pixmap2("images/set.png");
icon.addPixmap(pixmap2);
button2->setIcon(icon);
button2->setIconSize(QSize(50,50));
button2->setFixedSize(pixmap2.size());
button2->setMask(pixmap2.mask());
connect(button0,SIGNAL(clicked()),this,SLOT(slotbutton0()));
connect(button1,SIGNAL(clicked()),this,SLOT(slotbutton1()));
}
void Dialogs::slotbutton0()
{
execlp("sh","sh","hello.sh",NULL);
pid=getpid();
}
void Dialogs::slotbutton1()
{
char a[10];
sprintf(a,"%s",pid);
execlp("kill","kill",a,NULL);
}
/************************main.cpp*********************/
#include <QApplication>
#include<stdio.h>
#include<unistd.h>
#include <QtGui>
#include"dialog.h"
int main( int argc, char **argv )
{
QApplication a( argc, argv );
Dialogs *dialogs = new Dialogs();
// Dialogs *dialogs;
dialogs->show();
return a.exec();
}
刚接触qt,这个程序是有一个背景图片然后上面有几个按钮,刚开始我整合在一个main函数下面,但发现不能定义槽函数,然后用类的形式去改,执行后有段错误,这该继承什么类呢!
[最优解释]
QFrame *frame;
QPushButton * button0;
QPushButton * button1 ;
QPushButton * button2 ;
这些控件声明为指针,没有见到new。
声明了之后只是个指针,不是控件,需要new出来控件,使用空(野)指针肯定会段错误。
[其他解释]
段错误在哪行阿
[其他解释]
#include <QApplication>
#include<stdio.h>
#include<unistd.h>
#include <QtGui>
#include"dialog.h"
int pid;
Dialogs::Dialogs( )
{
frame=new QFrame(this);
button0=new QPushButton(this);
button1=new QPushButton(this);
button2=new QPushButton(this);
frame->setObjectName("myframe");
frame->resize(320,240);
frame->setStyleSheet("QFrame#myframe{border-image:url(images/background.png)}" );
button0->setGeometry(260,30,50,50);
button1->setGeometry(260,90,50,50);
button2->setGeometry(260,150,50,50);
QIcon icon;
QPixmap pixmap0("images/Start_amended.png");
icon.addPixmap(pixmap0);
button0->setIcon(icon);
button0->setIconSize(QSize(50,50));
button0->setFixedSize(pixmap0.size());
button0->setMask(pixmap0.mask());
QPixmap pixmap1("images/Stop_amended.png");
icon.addPixmap(pixmap1);
button1->setIcon(icon);
button1->setIconSize(QSize(50,50));
button1->setFixedSize(pixmap1.size());
button1->setMask(pixmap1.mask());
QPixmap pixmap2("images/set.png");
icon.addPixmap(pixmap2);
button2->setIcon(icon);
button2->setIconSize(QSize(50,50));
button2->setFixedSize(pixmap2.size());
button2->setMask(pixmap2.mask());
connect(button0,SIGNAL(clicked()),this,SLOT(slotbutton0()));
connect(button1,SIGNAL(clicked()),this,SLOT(slotbutton1()));
connect(button2,SIGNAL(clicked()),this,SLOT(quit()));
}
void Dialogs::slotbutton0()
{
execlp("sh","sh","hello.sh",NULL);
pid=getpid();
}
void Dialogs::slotbutton1()
{
char a[10];
sprintf(a,"%s",pid);
execlp("kill","kill",a,NULL);
}