Qstring to LPCWSTR - qt

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.

Related

QString::fromStdString returns broken text

In my Qt GUI app I use libwebrtc. From one on callbacks I want to emit signal with data, which is std::string. As core app logic use QString, I want convert std::string to Qstring.
I try following:
QString::fromStdString(stdstr)
QString::fromLatin1(stdstr.data())
Both of this return broken text, something like this
Only working way for me was
QString qstr;
for(uint i =0; i< stdstr.length(); i++)
qstr.append(stdstr.at(i));
Here my thoughts about reason of problem:
Encoding problems.
Binary problems
About encoding, libwebrtc should return std::string in UTF-8 by default, same as QString.
About binary. As I understand Qt built with GCC and corresponding stdlib, but libwebrtc build with CLang and libc++. For building I also specify usinng QMAKE_CXXFLAGS += -stdlib=libc++
What is correct way to convert types here?
UPD
I compare length of converted string and source string, they are very different.
For std::string I get 64, and for converted QString I get 5.
UPD2
Here is full function and corresponding slot for SPDGen signal
void foo(const webrtc::IceCandidateInterface *candidate) override {
std::string str;
candidate->ToString(&str);
QString qstr = QString::fromStdString(str);
qDebug() << qstr;
Q_EMIT SPDGen(qstr);
}
connect(conductor, &Conductor::SPDGen, this, [=](QString value){
ui->textEdit->setText(value);
});

How to solve undefined references, is the library not imported, or other problems

I'm trying to use Win32 GUI project on a project, but when I compile it, I get a lot of undefined reference errors.
||=== Build: Debug in 05.9.finally (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function Z4OpenP6HWND__':|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|61|undefined
reference towglCreateContext#4'|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|63|undefined
reference to wglMakeCurrent#8'|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|65|undefined
reference toglClearColor#16'| obj\Debug\main.o||In function
Z4Initv':|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|76|undefined
reference toglBlendFunc#8'|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|77|undefined
reference to glClearColor#16'|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|78|undefined
reference toglClearDepth#8'|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|79|undefined
reference to glDepthFunc#4'|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|80|undefined
reference toglEnable#4'|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|81|undefined
reference to glShadeModel#4'|
C:\Users\Administrator\Desktop\计算机图形\05.9.finally\main.cpp|82|undefined
reference toglHint#8'| obj\Debug\main.o||In function `Z6Draw3Dv':|
#include <GL/gl.h>
#include <GL/glut.h>
#include <windows.h>
GLfloat step = 0.0f;
HINSTANCE hInstance; // 系统实例句柄
HWND hWndMain; // 主窗体句柄
HWND hWnd; // 窗体句柄变量定义
HDC hDC; // 设备描述表变量定义
PIXELFORMATDESCRIPTOR pfd; // 像素格式结构变量定义
HGLRC hGLRC; // OpenGL渲染描述表变量定义
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // 窗体过程函数声明
GLboolean keys[256];
GLfloat rotStep = 30.0f;
void keyPress();
GLuint Open(HWND phWnd)
{
hWnd = phWnd;
hDC = GetDC(hWnd);
pfd.dwDamageMask = 0; // 忽略层遮罩
int pixelformat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixelformat, &pfd);
hGLRC = wglCreateContext(hDC);
wglMakeCurrent(hDC,hGLRC);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 初始化背景为灰色
return 0;
}
You includes have a forward slash in the naming. Maybe you wanted #include "path/name".
I believe you would have solved it by now; in case not then you can try providing correct library path. Use command line options -L for path to library folder and -l for library name while compiling/linking.
An online lookup for OpenGL suggests the library file names may be any or all from these: opengl32.a or opengl32.lib, glu32.a or glu32.lib, freeglut.a or freeglut.lib (choose .a if you are building on Linux or .lib on Windows).

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

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)

Linking error when including <pcl/io/png_io.h> in more than one file

I wanted to use pcl::io::savePNGFile in two source-files in my code.
As soon as I include the required include in second source-file
# include <pcl/io/png_io.h>
the project doesn't compile.
The error message is:
/usr/include/pcl-1.7/pcl/io/png_io.h:86: multiple definition of `pcl::io::saveRgbPNGFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char const*, int, int)'
I'm going to wrap the function in a class in order to include it only once in project. But I think it is not the best way. Am I doing something in a wrong way? Is there a better solution?
Thanks!
EDIT
Finally I've implemented a Q&D solution and wrapped the function (only for normal clouds)
cloudsaver.h
#ifndef CLOUDSAVER_H
#define CLOUDSAVER_H
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <string>
class CloudSaver
{
public:
CloudSaver();
void saveCloudToPNG(const std::string & fileName, const pcl::PointCloud<pcl::PointXYZRGBNormal>& cl );
};
#endif // CLOUDSAVER_H
cloudsaver.cpp
#include "cloudsaver.h"
# include <pcl/io/png_io.h>
CloudSaver::CloudSaver()
{
}
void CloudSaver::saveCloudToPNG(const std::string & fileName, const pcl::PointCloud<pcl::PointXYZRGBNormal>& cl )
{
pcl::io::savePNGFile<pcl::PointXYZRGBNormal>(fileName, cl );
}
But I'm still curious, how to do it properly.
As far as I know, There are some issues related to png_io.h.
I have change the definition of PCL_DEPRECATED in png_io.h file with this definition,and every thing becomes OK.
template <typename T>
PCL_DEPRECATED (void savePNGFile (const std::string& file_name, const pcl::PointCloud<T>& cloud),
"pcl::io::savePNGFile<typename T> (file_name, cloud) is deprecated, please use a new generic "
"function pcl::io::savePNGFile (file_name, cloud, field_name) with \"rgb\" as the field name."
);
look at this link [https://github.com/PointCloudLibrary/pcl/pull/300]
I guess you are using the static version of PCL.
To solve this issue you need to declare those methods as inline.
For example, for PCL 1.7.1, you need to edit this file:
pcl-pcl-1.7.1/io/include/pcl/io/png_io.h
And on these lines, add the keyword inline:
85: inline saveRgbPNGFile(...
96: inline savePNGFile(...
107: inline savePNGFile(...
119: inline savePNGFile(...
173: inline savePNGFile(...
Now rebuild the library, and you should be able to compile without any issues.

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());

Resources