Qt中通过QDialog来调用状态栏编译出现异常
这是dockdialog.cpp#include <QtGui>
#include <QEvent>
#include <QDockWidget>
#include <QDialog>
#include <QString>
#include <QStatusBar>
#include "dockdialog.h"
#include "ui_dockdialog.h"
Dockdialog::Dockdialog(QDialog *parent) :
QDialog(parent),
ui(new Ui::Dockdialog)
{
ui->setupUi(this);
setWindowTitle(tr("系统设置"));
setMouseTracking(true);
m_pStatus = new QStatusBar(this);
m_pStatus->setGeometry(QRect(20,300,571,41));
m_pStatus->showMessage("application init ok!");
pushButton->setMouseTracking(true); //进入某个按钮时,鼠标追踪属性失效,因此我们也需要激活该按钮的鼠标追踪功能
pushButton_2->setMouseTracking(true);
pushButton_3->setMouseTracking(true);
}
Dockdialog::~Dockdialog()
{
delete ui;
}
void Dockdialog::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void Dockdialog::mouseMoveEvent(QMouseEvent *e)
{
e->accept();
if(enterBtn(e->pos(), pushButton))
m_pStatus->showMessage (tr("Mouse Right Button Double Clicked:"));
if(enterBtn(e->pos(), pushButton_2))
// Qlabel->context->setText("这是第二个按钮");
m_pStatus->showMessage (tr("Mouse Right Button Double Clicked:"));
}
bool Dockdialog::enterBtn(QPoint pp, QToolButton *btn)
{
int height = btn->height();
int width = btn->width();
QPoint btnMinPos = btn->pos();
QPoint btnMaxPos = btn->pos();
btnMaxPos.setX(btn->pos().x()+width);
btnMaxPos.setY(btn->pos().y()+height);
if(pp.x() >= btnMinPos.x() && pp.y() >= btnMinPos.y()
&& pp.x() <= btnMaxPos.x() && pp.y() <= btnMaxPos.y())
return true;
else
return false;
}
这是dockdialog.h
#ifndef DOCKDIALOG_H
#define DOCKDIALOG_H
#include <QEvent>
#include <QDockWidget>
#include <QDialog>
#include <QPoint>
#include <QToolButton>
#include "ui_dockdialog.h"
namespace Ui {
class Dockdialog;
}
class QStatusBar;
class QMouseEvent;
class Dockdialog : public QDialog {
Q_OBJECT
public:
Dockdialog(QDialog *parent = 0 );
~Dockdialog();
void mouseMoveEvent(QMouseEvent * e);
bool enterBtn(QPoint , QToolButton *);
private slots:
protected:
void changeEvent(QEvent *e);
private:
Ui::Dockdialog *ui;
QString currentFileName;
QTimer *timer;
QStatusBar *m_pStatus;
;
QMouseEvent *e;
QToolButton *btn,*pushButton,*pushButton_2,*pushButton_3;
};
#endif // DOCKDIALOG_H
这是main函数
#include <QtGui/QApplication>
#include <QTextCodec>
#include <QFile>
#include "dockdialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));
Dockdialog dockdialog;
dockdialog.show();
return a.exec();
}
为什么编译出现
The program has unexpectedly finished. --------------------编程问答-------------------- 我后来调试的时候发现:setMouseTracking(true);
pushButton->setMouseTracking(true); 有冲突,收到异常信号;屏蔽了一个后,在if(enterBtn(e->pos(), pushButton))又收到异常信号中断了。
--------------------编程问答-------------------- 问题解决,内存溢出,那是因为那几个按钮已经在ui中定义,不用再在.h文件中定义了。 --------------------编程问答-------------------- 解决了就好了。。
补充:移动开发 , Qt