Error c2440: cannot convert const char[4] to char* - qt

i have a question:
I have a code in QT Creator (build in MSVC2013) that first work perfectly, now in other computer with an updated QT Creator (build in MSVC2015) when try tu rungive me this error:
"error: C2664: 'wave_object wave_init(char *)': cannot convert
argument 1 from 'const char [4]' to 'char *'" "Conversion from string
literal loses const qualifier (see /Zc:strictStrings)"
Error is in this part of the code:
wave_object db4;
wt_object wt;
db4 = wave_init("db4");
wt = wt_init(db4, "dwt", N, 4);
setDWTExtension(wt, "sym");
setWTConv(wt, "direct");
The definition of the method is here:
wave_object wave_init(char* wname) {
wave_object obj = NULL;
int retval;
retval = 0;
if (wname != NULL) {
retval = filtlength(wname);
}
I compile this with MinGW and dont have error, in the computer with QT Creator builded with MSVC2013 work fine also with the MSVC2013 compiler but now with the QT Creator builded with MSVC2015 appears this error with the MSVC2015 compiler.
I need to use the same compiler that was builded QT Creator because I am using QWT Plugins and is necessary to use the same compiler.
Thank you for your help! =)

In your function call:
db4 = wave_init("db4");
The "db4" argument is string literal, and is in a strict sense const.
Your definition of the function:
wave_object wave_init(char* wname)
does not identify parameter wname as const.
Modify as follows:
wave_object wave_init(const char* wname)

Related

Using a java method in Qt that returns double array

I want to use java code inside my Qt Android app.
So, I use a QAndroidJniObject with the method callMethod, and it is supposed to return a jdoubleArray.It takes a const char argument.
QAndroidJniObject javaCode = QAndroidJniObject("/myJavaCode");
jdoubleArray result = javaCode.callMethod<jdoubleArray>("detect", "([C)[D", buffer_java);
I don't know why but it doesn't compile, but I saw some examples with int that seemed to works fine.
error: undefined reference to '_jdoubleArray* QAndroidJniObject::callMethod<_jdoubleArray*>(char const*, char const*, ...) const'
Thanks for your answers
Pierre

QWebEngineView crashes on load() or page() method

I'm working on porting a Qt 5.5, QWebView project to Qt 5.6 (beta), QWebEngine. I've gone through the porting guide here. My code looks like this:
.h file defines _view like so:
QWebEngineView* _view;
and the .cpp constructor (class inherits from QWidget) has:
QVBoxLayout* vbox = new QVBoxLayout(this);
_view = new QWebEngineView(this);
connect(_view, SIGNAL(loadFinished(bool)), this, SLOT(loadPageFinished(bool)));
QString webDir = getReportBasePath() + no_tr("/index.html#") + startingPage;
// QWebEnginePage *page = _view->page(); // <-- CRASH!!
_view->load( QUrl("file:///" + webDir ) ); // <-- CRASH!!
_view->show();
vbox->addWidget(_view);
Upon doing either the page() or load() method, the whole thing crashes with:
Unhandled exception at 0x67019C66 (Qt5Cored.dll) in qxs.exe: 0xC0000005:
Access violation reading location 0x00000000.
I've verified that the _view pointer is not null.
If you look at the documentation, they have an example here that's almost identical to my code above. I've also tried replacing load() call to be identical to theirs:
view->load(QUrl("http://qt-project.org/"));
and it still crashes. Any ideas what could be causing these crashes?
Do I need to create a QWebEnginePage first and setPage() on the QWebEngineView? (I'm assuming not...) Could it have something to do with the Qt binaries (pre-built for Windows 32-bit MSVC 2013) that I'm using?
Relevant part of stack trace:
Qt5WebEngineWidgetsd.dll!QWebEnginePagePrivate::QWebEnginePagePrivate(QWebEngineProfile * _profile) Line 95 C++
Qt5WebEngineWidgetsd.dll!QWebEnginePage::QWebEnginePage(QObject * parent) Line 393 C++
Qt5WebEngineWidgetsd.dll!QWebEngineView::page() Line 145 C++
Qt5WebEngineWidgetsd.dll!QWebEngineView::load(const QUrl & url) Line 157 C++
qxs.exe!ReportWidget::ReportWidget(QcaMain * qm, QWidget * parent, QString startingPage) Line 321 C++
It crashes here:
QWebEnginePagePrivate::QWebEnginePagePrivate(QWebEngineProfile *_profile)
: adapter(new WebContentsAdapter)
, history(new QWebEngineHistory(new QWebEngineHistoryPrivate(this)))
, profile(_profile ? _profile : QWebEngineProfile::defaultProfile())
, settings(new QWebEngineSettings(profile->settings()))
, view(0)
, isLoading(false)
, scriptCollection(new QWebEngineScriptCollectionPrivate(browserContextAdapter()->userScriptController(), adapter.data()))
, m_backgroundColor(Qt::white)
, fullscreenMode(false)
{
memset(actions, 0, sizeof(actions));
}
I thought maybe that had something to do with _profile being NULL, so I attempted to first set a QWebEngineProfile like so:
QWebEnginePage* page = new QWebEnginePage( QWebEngineProfile::defaultProfile(), _view );
_view->setPage( page );
Then it instead crashes in qwebengineprofile.cpp here:
static QWebEngineProfile* profile = new QWebEngineProfile(
new QWebEngineProfilePrivate(BrowserContextAdapter::defaultContext()),
BrowserContextAdapter::globalQObjectRoot());
with stack trace:
Qt5Cored.dll!convert_to_wchar_t_elided(wchar_t * d, unsigned int space, const char * s) Line 256 C++
Qt5Cored.dll!qt_message_fatal(QtMsgType __formal, const QMessageLogContext & context, const QString & message) Line 1593 C++
Qt5Cored.dll!QMessageLogger::fatal(const char * msg, ...) Line 784 C++
Qt5WebEngineCored.dll!`anonymous namespace'::subProcessPath(void) C++
Qt5WebEngineCored.dll!WebEngineLibraryInfo::getPath(int) C++
Qt5WebEngineCored.dll!WebEngineContext::WebEngineContext(void) C++
Qt5WebEngineCored.dll!WebEngineContext::current(void) C++
Qt5WebEngineCored.dll!QtWebEngineCore::BrowserContextAdapter::defaultContext(void) C++
> Qt5WebEngineWidgetsd.dll!QWebEngineProfile::defaultProfile() Line 516 C++
Problem solved. I was missing some key files required by QWebEngine, otherwise it'll crash. These files must be in the same directory as the executable. They are put there by the windeployqt.exe tool, so that's the best way to ensure your Qt application has everything it needs to run without crashing.
qtwebengine_resources.pak
qtwebengine_resources_100p
qtwebengine_resources_200p.pak.pak
QtWebEngineProcess.exe
icudtl.dat
The reason this got me was our development group was formerly using Qt 4.8, and uses an in-house method to copy the required Qt dlls and whatnot into the target directory. When upgrading to Qt 5.x and adding QWebEngine, we didn't realize the files above were required.
You can set Windows environment variable called QTWEBENGINEPROCESS_PATH
Example:
QTWEBENGINEPROCESS_PATH C:\Qt\Qt5.6.0\5.6\msvc2013_64\bin\QtWebEngineProcess.exe
With this solution, no need to copy resources files into your projects output folders

Qstring to LPCWSTR

LPCWSTR path;
void WinApiLibrary::StartProcess(QString name)
{
path = name.utf16();
CreateProcess(path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}
C:\kursovaya\smc\winapilibrary.cpp:21: error: invalid conversion from
'const ushort* {aka const short unsigned int*}' to 'LPCWSTR {aka const
wchar_t*}' [-fpermissive]
path = name.utf16();
This code worked in the Qt 4.8, but now i have Qt 5.2 and this code isn't working. What's wrong with this guy?
I had the same issue (I'm using Qt 5.3), this is how I fixed it:
QString strVariable1;
LPCWSTR strVariable2 = (const wchar_t*) strVariable1.utf16();
QString::utf16() returns const ushort*, which is different from const wchar_t*, so you have the compile error.
You are probably compiling with /Zc:wchar_t. If you change it to /Zc:wchar_t- it should work, as wchar_t type becomes typedef to 16-bit integer in this case.
In Visual Studio: Project Properties->Configuration Properties->C/C++->Treat WChar_t As Built in Type->No.
Or just add reinterpret_cast<LPCWSTR>.
I'm using Qt 5.2 and I had the same issue. This is how I fixed it:
QString sPath = "C:\\Program File\\MyProg";
wchar_t wcPath[1024];
int iLen = sPath.toWCharArray(wcPath);
In the Qt global namespace, there is the macro qUtf16Printable. However as the documentation states, this produces a temporary so take care to use it properly. Note this was introduced in Qt5.7.

force call to QString(const char *) constructor in Qt 4.7

I am trying to compile a library written in Qt 4.6. On my current Linux machine I have only Qt 4.7 installed. The following code part:
/*file try.h*/
void fileOpen(QString s = NULL) ;
/*file try.cpp*/
void MainWindow::fileOpen(QString s) {
QString filename ;
if(s.isNull()) filename = QFileDialog::getOpenFileName(
this,
"Choose a file",
".",
"Source file (*.)");
else filename = s ;
}
compiles with the following error (I used cmake but the corresponding line code is the one listed above):
In member function ‘virtual int MainWindow::qt_metacall(QMetaObject::Call, int,
void**)’:
/homes/combi/hodorog/Developments/axelOld/build/axel/src/QGui/moc_MainWindow.cxx:141:26:
error: conversion from ‘long int’ to ‘QString’ is ambiguous
/homes/combi/hodorog/Developments/axelOld/build/axel/src/QGui/moc_MainWindow.cxx:141:26:
note: candidates are:
/usr/include/QtCore/qstring.h:426:43: note: QString::QString(const char*)
/usr/include/QtCore/qstring.h:105:14: note: QString::QString(const QChar*)
So I am guessing the problem is that in qt. 4.7. there are two QString constructors that can take a pointer as an argument (as listed in the compilation error), whereas in qt 4.6. there is only one QString constructor that can take a pointer as an argument. How can I force QString to call the constructor with const char * as an argument?
Thank a lot for your help in advance,
madalina
void fileOpen(QString s = NULL);
You are trying to construct a QString object with 0. It seems you are confusing the null of pointers with a null QString. A null QString is one which is created with the constructor QString(). Given how your function is implemented (referring to s.isNull()), you should change the function declaration to
void fileOpen(QString s = QString());

Qt json parser return parse error

I try to parse json string using this method:
QString ourJSONData = QString('{"couchdb":"Welcome","version":"1.0.1"}');
QString response = QString("[") + QString(ourJSONData) + QString("]");
QScriptEngine engine;
QScriptValue sc = engine.evaluate(response);
ui->label->setText(sc.toString());
But label return
SyntaxError: Parse error
I using Qt 4.7.4
What i do wrong? Thanks.
UPD:
Sorry, problem was in that string:
QString ourJSONData = QString('{"couchdb":"Welcome","version":"1.0.1"}');
need change to:
QString ourJSONData = QString("{\"couchdb\":\"Welcome\",\"version\":\"1.0.1\"}");
P.S. this method i found at http://blog.siegerstein.com/archives/134
I built your code in QtCreator in got a very helpful error message:
character constant too long for its type
It's because your ourJSONData variable is initialised with a text in single quotes, which is for single characters.
This will correct that initialisation. (I put a \ before each double-quote, and then changed the single-quotes to double):
QString ourJSONData = QString("{\"couchdb\":\"Welcome\",\"version\":\"1.0.1\"}");

Resources