我在Qt中构建了一个包含两个按钮的应用程序:退出按钮和导入按钮。按下导入按钮时,屏幕上的滚动区域中会显示一个按钮列表(文件loggers.csv包含数据1; 2; 3; 4; 5;)。
一切正常,但是当我按下退出按钮(当然应该关闭所有内容)时,应用程序没有正确停止(Qt的停止按钮仍处于活动状态,播放按钮不是)。当我运行调试器并按下退出按钮时,它会给出一个错误:为RtlFreeHeap(0ADF0000,0028FE40)指定的地址无效。有人能帮助我吗?
主要
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showFullScreen();
return a.exec();
}
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
#include "logger.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton exit_btn;
QPushButton import_btn;
private slots:
void createMenus();
void exit();
void import();
private:
int window_width;
int window_height;
int numLoggers;
int numSelected;
QVector<Logger*> loggers;
QScrollArea * scroll_area;
QVBoxLayout scrollLayout;
QWidget viewport;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QtGui"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
window_width = QApplication::desktop()->width();
window_height = QApplication::desktop()->height();
createMenus();
connect(&exit_btn,SIGNAL(clicked()),this,SLOT(exit()));
connect(&import_btn,SIGNAL(clicked()),this,SLOT(import()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createMenus()
{
import_btn.setParent(ui->centralWidget);
import_btn.setGeometry(400,300,100,100);
import_btn.setText("IMPORT");
exit_btn.setText("EXIT");
exit_btn.setParent(ui->centralWidget);
exit_btn.setGeometry(window_width-50,12,32,32);
viewport.setLayout(&scrollLayout);
viewport.resize(0,0);
scroll_area = new QScrollArea(ui->centralWidget);
scroll_area->setGeometry(0,66,317,window_height-116);
scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scroll_area->setWidget(&viewport);
scroll_area->setGeometry(0,97,317,window_height-228);
scrollLayout.setMargin(0);
scrollLayout.setSpacing(0);
}
void MainWindow::exit()
{
close();
qApp->quit();
}
void MainWindow::import()
{
numSelected=0;
QFile f("Loggers3.csv");
if (f.open(QIODevice::ReadOnly))
{
numLoggers=0;
QString data;
data = f.readAll();
QStringList vals = data.split(';');
while(vals.size()>=1)
{
Logger * logger = new Logger;
logger->setNumber(vals[0].toInt());
vals.removeAt(0);
loggers<<logger;
numLoggers++;
}
f.close();
for(int i=0; i<numLoggers;i++)
{
loggers[i]->createButtons();
scrollLayout.addWidget(loggers[i]->button);
}
viewport.resize(367,numLoggers*60);
}
}
logger.h
#ifndef LOGGER_H
#define LOGGER_H
#include <QtGui>
class Logger : public QWidget
{
Q_OBJECT
public:
explicit Logger(QWidget *parent = 0);
~Logger();
int number;
QLabel num;
QToolButton * button;
bool checked;
signals:
public slots:
void setNumber(int number);
void createButtons();
};
#endif // LOGGER_H
logger.cpp
#include "logger.h"
#include <QtGui>
Logger::Logger(QWidget *parent) :
QWidget(parent)
{
button = new QToolButton;
button->setCheckable(true);
button->setMinimumSize(317,60);
button->setStyleSheet("QToolButton{background-image: url(images/btn_bg); border:none}");
}
Logger::~Logger()
{
}
void Logger::setNumber(int logNumber)
{
number=logNumber;
}
void Logger::createButtons()
{
QLayout * layout = new QHBoxLayout;
QSpacerItem *spacer = new QSpacerItem(120, 31, QSizePolicy::Maximum, SizePolicy::Maximum);
num.setStyleSheet("color: white; font: bold 16px");
num.setText(QString::number(number));
layout->addWidget(&num);
layout->addItem(spacer);
button->setLayout(layout);
}
答案 0 :(得分:6)
我不完全确定你想要实现的目标......但你的问题在于这两行:
viewport.setLayout(&scrollLayout);
viewport.resize(0,0);
在QWidget类的文档中,它声明:
如果此小部件上已安装了布局管理器,则为QWidget 不会让你安装另一个。您必须首先删除现有布局管理器(由layout()返回),然后才能使用新布局调用setLayout()。
这就是你的问题所在。不要相信我,在这两行代码之前加上这个检查。
if(layout()){
qDebug() << "Another layout exists";
}
来源:QVBoxLayout Class Reference
QVBoxLayout类垂直排列小部件。
此类用于构造垂直框布局对象。有关详细信息,请参阅QBoxLayout。
最简单的使用类是这样的:
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
window->setLayout(layout);
window->show();
首先,我们在布局中创建我们想要的小部件。然后,我们创建QVBoxLayout对象并将小部件添加到布局中。最后,我们调用QWidget :: setLayout()将QVBoxLayout对象安装到窗口小部件上。此时,布局中的小部件被重新设置为将窗口作为其父级。
项目中的重要错误来源:
小部件应该在堆上构建,因为它们将在删除父级时自动删除。 您有一个在堆上实例化的自定义窗口小部件类。成员们也应该上堆。此外,您应该考虑在GUI代码中使用父/子层次结构,以确保正确的内存管理和正确删除。
答案 1 :(得分:1)
根据我的经验,如果你的程序在RtlFreeHeap
停止,那么这是记忆损坏的好兆头。
致电
import_btn.setParent(ui->centralWidget);
centralWidget
取得import_btn
的所有权。这意味着,当centralWidget
被删除(在delete ui;
的析构函数中作为MainWindow
的一部分发生)时,它会在您的成员变量上调用delete
!
这导致报告的内存损坏。
您需要动态分配QPushButton
,而不是普通成员变量。所以让他们QPushButton*
。
答案 2 :(得分:0)
以下是我在mainwindow.cpp中的表现,感谢和这个问题:How to create a correct exit button in qt
QPushButton * quit_btn = new QPushButton(this);
quit_btn->setGeometry(540,440,93,27);
quit_btn->setText("Exit");
QObject::connect(quit_btn,SIGNAL(clicked()),qApp,SLOT(quit()));
完美无瑕地工作:D