I m using Ubuntu 20.04 with Qt5.12 for developing a QT project which uses a QVTK widget to display a 3D PCL visualiser. I m facing memory leak issues after defining the visualiser and vtk widget relationship. The header file contents are as follows:
class SiLS : public QMainWindow
{
Q_OBJECT
public:
SiLS(QWidget *parent = nullptr);
~SiLS();
private:
Ui::SiLS *ui;
pcl::visualization::PCLVisualizer::Ptr viewer_3D;
};
The cpp file contents are as follows:
SiLS::SiLS(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::SiLS)
{
ui->setupUi(this);
viewer_3D.reset (new pcl::visualization::PCLVisualizer ("viewer_3D", false));
viewer_3D->setupInteractor (ui->qvtkWidget->GetInteractor (), ui->qvtkWidget->GetRenderWindow ());
ui->qvtkWidget->SetRenderWindow(viewer_3D->getRenderWindow());
}
SiLS::~SiLS()
{
delete ui;
}
Only these 3 lines of code in constructor is giving lot of memory leak issues. Following screenshot shows few of the leaks identified by valgrind in Qt creator.
SiLS.cpp:10:0 represents the following line in cpp file:
viewer_3D.reset (new pcl::visualization::PCLVisualizer ("viewer_3D", false));
Related
I'm using Qt 6.4 on Visual Studio 2022, I'm trying to understand how to add a new .ui to an existing project.
For example, I have a project with a mainwindow.ui, when I open it on Qt Designer and create a new .ui window from:
[Qt Designer] File -> New -> Main Window -> Create
I added some widgets to this new window and saved it to disk with the name new_window.ui.
How do I include it in my current project? I tried #include "new_window.ui" but It throws a lot of compiler syntax errors.
What I'm trying to achieve is access the widgets from new_window.ui inside of my MainWindow class:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);
// access the 'ui' from new_window.ui here
// new_window_ui.pushButton-> ...
}
I have seen that this ui generally is a private class member, there's no problem in having the new_window.ui ui public.
What is the correct way to achieve it?
I have been able to include the new ui file by creating it on a new project and copying the generated ui_new_window.h file plus her class header:
// new_window.h
#pragma once
#include "stdafx.h"
#include "ui_new_window.h"
QT_BEGIN_NAMESPACE
namespace Ui { class new_windowClass; };
QT_END_NAMESPACE
class new_window : public QWidget
{
Q_OBJECT
public:
new_window(QWidget *parent = nullptr) : QWidget(parent), ui(new Ui::new_windowClass())
{
ui->setupUi(this);
}
~new_window() { delete ui; };
Ui::new_windowClass *ui;
};
When using QMedaiPlayer in QT to play MP4 files, the build passes normally, and the program quits abnormally during runtime, which has troubled me for many days. I hope everyone can help!
There is no problem with the build, and it exits abnormally during runtime.
The replacement of a smaller MP4 file still does not work, so it is not a matter of MP4 file size.
The use of MKV files will not work, nor is it a file format issue.
The version is QT 5.12.8
This is the mainwindow.cpp file
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow){
this->setFixedSize(1024,576);
ui->setupUi(this);
QHBoxLayout *m_layout= new QHBoxLayout(this);
QMediaPlayer *m_player = new QMediaPlayer(this);
m_player->setMedia(QUrl::fromLocalFile(QString::fromLocal8Bit("D:/try.mkv")));
m_videoW = new QVideoWidget(this);
m_layout->addWidget(m_videoW);
this->setLayout(m_layout);
m_player->setVideoOutput(m_videoW);
m_videoW->show();
m_player->play();
qDebug()<<"hello";
}
void MainWindow::paintEvent(QPaintEvent *){
QPainter painter(this);
painter.drawPixmap(0,0,width(),height(), m_videoW->currentPixmap());
}
This is the mainwindow.h file
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
void paintEvent(QPaintEvent*);
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QVideoWidget *m_videoW;
};
The corresponding library is also added to the pro file:
QT += core gui
QT += multimedia
QT += multimediawidgets
I never found where the problem is, please help me find it! Thank you!
Test with qDebug, hello can output normally.
*The code has been displayed according to the principle of least recurrence.
i have the following situation:
InspectionTreeWidget header:
class InspectionTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
explicit InspectionTreeWidget(QWidget *parent = 0);
private:
QVBoxLayout *layout_sourcesettings;
QFrame *frame_sourcesettings;
...
InspectionTreeWidget implementation:
InspectionTreeWidget::InspectionTreeWidget(QWidget *parent) :
QTreeWidget(parent)
{
QVBoxLayout *layout_sourcesettings;
layout_sourcesettings= new QVBoxLayout();
frame_sourcesettings=new QFrame(this);
frame_sourcesettings->setLayout(layout_sourcesettings);
...
}
everything works well but when i close the application, i get the error "Heap corruption detected after normal block"
what i'm doing wrong?
Thanks
Edit (Solved):
I don't know if it is a bug or not but the problem was happening because of a sequence in build process:
first i had this code:
InspectionTreeWidget::InspectionTreeWidget(QWidget *parent) :
QTreeWidget(parent)
{
QVBoxLayout* layout_sourcesettings= new QVBoxLayout();
frame_sourcesettings=new QFrame(this);
frame_sourcesettings->setLayout(layout_sourcesettings);
...
}
i did a rebuild and everythink was ok.
then i move the declaration of layout_sourcesettings to the header and i build the project and got not errors compiling but when i close the application i got the heap corruption. But after a rebuild i no longer get the heap corruption...
So my question now is: is this a Qt related bug or everytime i move local declarations to header file i have to do a rebuild?
Thanks
So I'm making a web browser as my first Qt project (surprise!) and I'm wondering why calling setWindowState(Qt::WindowMaximized) is not changing window geometry. I have this code:
From mainwindow.h:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
Ui::MainWindow *ui;
};
From mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// this->geometry() is the same here...
setWindowState(Qt::WindowMaximized);
ui->webView->setGeometry(0, 60, geometry().width(), geometry().height()-60);
// ...as it is here.
}
As you may be able to tell, I'm trying to start the application with the window maximized and the QWebView also maximized. Basically, whenever the main window is resized, I also want to call ui->webView->setGeometry with the update height and width. But MainWindow::geometry doesn't seem to be updating. What am I doing wrong?
I would have to double check, but your geometry might not get updated properly until your main window gets a show event.
However, I would suggest you put your QWebView inside of a layout instead of trying to size it manually every time your main window changes size.
I am new to Qt Framework...
I want to display a .png pic in my Form1.ui ,
so I dragged and dropped a Graphic view from the widget box then
I placed test.png in the same directory (inside the project folder)
and i did this in the code
//Form1.cpp
#include "form1.h"
#include "ui_form1.h"
Form1::Form1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form1)
{
ui->setupUi(this);
ui->Pic1->setStyleSheet("background-image: url(test.png)");
}
Form1::~Form1()
{
delete ui;
}
//Form1.h
#ifndef FORM1_H
#define FORM1_H
#include <QWidget>
namespace Ui {
class Form1;
}
class Form1 : public QWidget
{
Q_OBJECT
public:
explicit Form1(QWidget *parent = 0);
~Form1();
private:
Ui::Form1 *ui;
};
#endif // FORM1_H
It compiled perfectly but the pic didn't appear ,
What did I Wrong ?
this is my qrc :
The Widget you should use to show pictures is a QLabel. You can do it directly from QtCreator, by setting its pixmap property.
As others said, you should first create a resource file and then add the image to that resource file. To create a Qt Resource File, go to the menus: File > Qt > Qt Resource File.
EDIT To do it programatically:
//names starting with : means that they are on a resource file,
//otherwise in the filesystem
QPixmap * mypix = new QPixmap(":/karim/test.png");
ui->your_label->setPixmap(mypix);
delete mypix;
If you have the png in your resources, maybe change your background-image: tag:
ui->Pic1->setStyleSheet("background-image: url(:/test.png)");
You need to add the image to a resource file: http://doc.qt.io/qt-5/resources.html