Segfault when adding QwtPlot to the code - c++

I try to use QwtPlot, but when i add this line to my MainWindow.cpp
QwtPlot *plot = new QwtPlot(QwtText("Demo"), this);
the application compile and link without errors, but when I try to run it I get
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff514227c in ?? () from /usr/lib/libQtGui.so.4
without any backtrace. My .pro file:
INCLUDEPATH += /usr/include/qwt
CONFIG += qwt
LIBS += -lqwt
I'm using Qwt 6.0.2, Qt Creator 2.7.0 and have Qt 4.8.4 and 5.0.2 installed.
The error also occours when I create a "Qt Gui Application" (without .ui file) and just this code:
qwt-test.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qwt-test
TEMPLATE = app
INCLUDEPATH += /usr/include/qwt
CONFIG += qwt
LIBS += -lqwt
SOURCES += main.cpp\
MainWindow.cpp
HEADERS += MainWindow.hpp
main.cpp
#include "MainWindow.hpp"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug() << "main";
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow.hpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_HPP
MainWindow.cpp
// MainWindow.cpp
#include "MainWindow.hpp"
#include <qwt_plot.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QwtPlot *plot = new QwtPlot(QwtText("Demo"), this);
}
MainWindow::~MainWindow()
{
}
Thanks!

It was a problem with Qt Creator (or Qwt being incompatible with Qt 5), it recognized qmake as qmake for Qt 4 but it was for Qt 5. Fixing the versions in Options -> Build&Run -> Qt Versions and using Qt 4 for the project fixed the segfault.

Related

Regarding Projector Programming using QT

I am connecting the projector to Quectel QCOM application. Now I want to skip that application and create my customized application to control the projector.
I wrote a program based on the guidelines of Quectel to switch off the projector. Following is the code. Even I used the QSerialPort code. But the projector couldn't switch off. However using the command prompt of the Quectel QCOM application, I could control the projector.
Please help me in this guys! I am stuck in this since a month.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec(); //
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include<QSerialPort>
#include<string>
#include<QtGui>
#include <QMessageBox>
using namespace std;
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow() //
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QMessageBox::information(this,"Title here","Hello World");
serial = new QSerialPort(this);
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::NoParity);
if(!serial->open(QIODevice::ReadWrite))
{
qDebug()<<"error: can't open com port";
}
QString command = "WT+LEDE=0\n";
QByteArray x = command.toLocal8Bit();
serial->write(x);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
QMake project file:
#-------------------------------------------------
Project created by QtCreator 2019-11-02T10:55:21
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GUISerialPort
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui

Memory leak in QWebView

I am using Qt5.4.3 and at some point, Qt prints out message like this
LEAK: 145 CachedResource
LEAK: 4432 WebCoreNode
I try to display a webpage on a Qt application, but whatever the webpage is, memory always leaks.
Here is the all code of project named"test"(I add the webview control in mainwindow.ui)
#test.pro
QT += core gui webkit
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets webkitwidgets multimedia multimediawidgets
TARGET = EchartDemo
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
INCLUDEPATH += $$PWD
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
DESTDIR = bin
//main.cpp
#include "widget.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.showMaximized();
return a.exec();
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//`widget.cpp`
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->webView->load(QUrl("file:///"+qApp->applicationDirPath()+"/html/hao.html"));
}
Widget::~Widget()
{
delete ui;
}
//hao.html
<meta http-equiv="Refresh" content="0; url=http://www.hao123.com/?1460255739"/><meta property="shurufa:url-navigate" content="985" />
In order to show the webpage normally, you should put the html file to the folder \build-EchartDemo-Desktop_Qt_5_4_2_MinGW_32bit-Debug\bin\html.
Of course, you can change the content of the hao.html file to whatever you like.
Why do I have these memory leaks?
This is a known bug in Qt and these "memory leaks" are only warnings in some cases. You can read more about it here : Qt Bug 40373, and also see the other bug reports mentioning those leaks.
Also, you should consider to use QWebEngineView which is much better ;) (and update to Qt5.6, but it is another story!).

QListView not displaying in Mac OS

I am trying to display a very simple and short list of countries using QStringListModel and QListView. But when I compile it, I get a blank window.
This is the code for my class :
#include "FenPrincipale.h"
FenPrincipale::FenPrincipale()
{
QVBoxLayout *layout = new QVBoxLayout;
QStringList listePays;
listePays << "France" << "Espagne" << "Italie" << "Portugal" << "Suisse";
QStringListModel *modele = new QStringListModel(listePays);
QListView *vue = new QListView;
vue->setModel(modele);
layout->addWidget(vue);
setLayout(layout);
}
The header :
#ifndef FENPRINCIPALE_H
#define FENPRINCIPALE_H
#include <QMainWindow>
#include <QVBoxLayout>
#include <QStringList>
#include <QListView>
#include <QStringListModel>
class FenPrincipale : public QMainWindow
{
Q_OBJECT
public:
FenPrincipale();
};
#endif // FENPRINCIPALE_H
The main :
#include "FenPrincipale.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FenPrincipale w;
w.show();
return a.exec();
}
The pro file :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = utilisationModeleSimple
TEMPLATE = app
SOURCES += main.cpp\
FenPrincipale.cpp
HEADERS += FenPrincipale.h
FORMS += FenPrincipale.ui
I am currently using Qt Creator 3.3.0 based on Qt 5.4.0 (Clang 6.0 (Apple), 64 bit); OS X Yosemite 10.10.2
What is the problem with this code?
If you want to use QMainWindow, then you need to set your widget as central: QMainWindow::​setCentralWidget. There are good samples in Qt documentation.
FenPrincipale::FenPrincipale()
{
QStringList listePays;
listePays << "France" << "Espagne" << "Italie" << "Portugal" << "Suisse";
QStringListModel *modele = new QStringListModel(listePays);
QListView *vue = new QListView;
vue->setModel(modele);
setCentralWidget( vue );
}
If you don't need QMainWindow functionality, then simply interhit QWidget instead of QMainWindow - and your code will work.

Canon EDSDK Crashes after start

I am trying to write a simple camara app. I created a new Project and add the EDSDK headers and the lib. Also i have added the init command EdsInitializeSDK().
test.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
LIBS += -L".\EDSDK.lib"
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <EDSDK.h>
#include <EDSDKErrors.h>
#include <EDSDKTypes.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
EdsError err=EDS_ERR_OK;
err = EdsInitializeSDK();
}
MainWindow::~MainWindow()
{
delete ui;
}
the main.cpp and mainwindow.h are not modified.
it compiled, but when i want to start the programm it crash without any error.
In debug i become the error 0xc0000135
I use the Canon_Camara_SDK_Kit_v2_14 with Qt on Win 7.
The EDSDK.lib are in the src and in the build Folder.
I have no Idea whats wrong
Thanks!

Getting runtime errors with QWebPage

I have created a Qt GUI application but I haven't touched anything regarding the GUI. I have modified mainwindow.cpp and the project file.
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebPage>
#include <QWebFrame>
QWebPage page;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(page.mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded1(bool)));
QUrl router("http://192.168.1.1");
page.mainFrame()->load(router);
}
MainWindow::~MainWindow()
{
delete ui;
}
untitled.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2013-05-01T23:48:00
#
#-------------------------------------------------
QT += core gui webkit webkitwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Error:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!
Program: ...tled-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\debug\untitled.exe
Module: 5.0.2
File: global\qglobal.cpp
Line: 1977
ASSERT: "!"No style available without QApplication!"" in file kernel\qapplication.cpp, line 962
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Extra characters are inserted here to bypass character requirement.
In main.cpp, make sure you create an application object, even if you don't use directly:
QApplication app;
// Below you can then create the window
Edit
The problem is that you are creating a QWebPage as a global object, and before the QApplication has been created. To solve the problem, make the page a member of the MainWindow class. Also make the page a pointer, otherwise you will get other problems.
i.e. in mainwindow.h:
private:
QWebPage* page;
in mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebPage>
#include <QWebFrame>
// Remove this!!
// QWebPage page;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Create the page here:
page = new QWebPage(this);
connect(page.mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded1(bool)));
QUrl router("http://192.168.1.1");
page.mainFrame()->load(router);
}
MainWindow::~MainWindow()
{
delete ui;
}