Move semantics in Qt without pointers? - qt

I have a Qt project, there I have an Object, which is going to be copied a lot of time. Therefor I would like to add move semantics.
#ifndef OBJECTTOCOPY_H
#define OBJECTTOCOPY_H
#include <QColor>
#include <QString>
#include <QDataStream>
namespace level_1 {
namespace level_2 {
class ObjectToCopy {
public:
explicit ObjectToCopy(const QString& _name = "", const QColor& colorBody = QColor() );
// MOVE
ObjectToCopy(ObjectToCopy && other);
static quint32 _valueInt32;
static quint16 _valueInt16;
QString _name;
QColor _colorBody;
private:
};
}
}
#endif // OBJECTTOCOPY_H
How do I steal the pointers of the member variables, since they are no pointers?
ObjectToCopy::ObjectToCopy (ObjectToCopy&& other)
: _valueInt32( other._valueInt32 )
, _valueInt16( other._valueInt16 )
, _name( other._name )
, _colorBody( other._colorBody )
{
other._valueInt32 = 0;
other._valueInt16 = 0;
other.name.clear();
other._colorBody = QColor();
}
Does that make sense for non-pointers?
Is it ok to reset QString 's like string.clear(); to mark that for the garbage collector?
How could I reset a QColor object?

You can add move semantics of course, but in your case there is no need in this at all. quint32, quint16 are moved by copying. QColor is wrapper around union and has no move constructor (and doesn't need one) and will also be moved by copying. QString is reference counted type in QT. It has move constructor in recent versions of library, but the difference in speed will be minimal (difference between swapping pointer and incrementing reference counter).

You are looking for std::move:
ObjectToCopy::ObjectToCopy (ObjectToCopy&& other)
: _valueInt32( other._valueInt32 )
, _valueInt16( other._valueInt16 )
, _name( std::move(other._name) )
, _colorBody( std::move(other._colorBody) )
{
other._valueInt32 = 0; //probably not necessary
other._valueInt16 = 0; //probably not necessary
//other.name.clear(); //not necessary
//other._colorBody = nullptr; //not necessary
}
It makes sense to move non-pointers. You are in the process of making such an object. Moving integers doesn't help, but doesn't hurt either, so you may as well do it for consistancy. Moving things that don't have a move constructor also works: If no move constructor is available the copy constructor is used (moving is not always better, but not worse either).
The implementation above says "move by copying the ints and moving the name and the _colorBody".
Make sure you do not read from variables you moved from.
It is ok, but not necessary. other is supposed to be a temporary that will get destroyed anyways. (C++ does not have a garbage collector in your sense)
Also once an object is moved from it tends to be in the cleared state like for QString, but that is not always the case.
You cannot really. You can assign a default constructed one like other._colorBody = QColor(); but that just means it sets the color to black. A QColor cannot be empty, it always has some color.
Also read What are move semantics?

Related

QTextLayout / QTextLine support grouping of several-characters so it acts as one character for the cursor

Is it possible for QTextLayout to render several characters, but to process/handle it as one character. For example rendering a code point like: [U+202e], and when moving the caret/calculating positions, it is treated as one character.
Edited:
Please check this following issue, were I explain what I'm trying to do. It for the edbee Qt component. It's using QTextLayout for line rendering.
https://github.com/edbee/edbee-lib/issues/127
Possibly it isn't possible with QTextLayout, the documentation is quite limited.
According to Qt docs:
"The class has a rather low level API and unless you intend to implement your own text rendering for some specialized widget, you probably won't need to use it directly." - https://doc.qt.io/qt-5/qtextlayout.html#details
You should probably use a QLineEdit or a QTextEdit (each has a method called setReadOnly(bool)).
Before answering the question, I will point out that the CursorMode enum (https://doc.qt.io/qt-5/qtextlayout.html#CursorMode-enum) seems very promising for this problem, but to me, the documentation isn't clear on how to use it or set it.
Now to answer your question in regards to QLineEdit or QTextEdit, it's a bit complicated, but it's the same for QLineEdit and QTextEdit, so lets look at QTextEdit.
Firstly, mouse clicks: QTextEdit has a signal called cursorPositionChanged(), which will be helpful here. You'll want to connect that to a custom slot, which can make use of the function moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor) (https://doc.qt.io/qt-5/qtextedit.html#moveCursor). Notice that there are very helpful enumeration values for you here in QTextCursor::MoveOperation regarding word hopping (https://doc.qt.io/qt-5/qtextcursor.html#MoveOperation-enum). How do we put all of this together? Well, probably the right way to do it is to determine the width of the chars to the left of the cursor's position and the width of the chars to the right of the cursor's position when the cursorPositionChanged() signal is emitted and go to the side of the word that has less width. However, I'm not sure how to do that. At this point I'd settle with checking the number of chars to the left and right and going to the side with less.
Secondly, keyboard presses: This goes a bit out of my knowledge, but almost everything drawable and iteractable inherits from QWidget. Take a look at https://doc.qt.io/qt-5/qwidget.html#keyPressEvent and it's possible that overriding that in your own implementation of QTextEdit is necessary to get the left arrow and right arrow keypresses to jump words (once you get that part it's pretty easy, just use the same function as last section for moving the cursor, or in the case of QLineEdit, cursorWordForward()/cursorWordBackward()).
All this being said, I've so far been assuming that you're not deleting anything or selecting anything. Selection can be a real pain depending on if you allow multiple selections, but the functions are all there in the documentation to implement those things.
Example of mouse click impl:
myclass.hpp
#include <QTextEdit>
#include <QTextCursor>
#include <QObject>
#include <QString>
int distance_to_word_beginning_or_end(const QString &str, int index, bool beginning);
class MyClass {
MyClass();
~MyClass();
private:
QTextEdit *text_edit;
public slots:
void text_edit_changed_cursor_location();
};
myclass.cpp
#include "myclass.hpp"
int distance_to_word_beginning_or_end(const QString &str, int index, bool beginning)
{
// return the distance from the beginning or end of the word from the index given
int inc_or_dec = (beginning) ? -1 : 1;
int distance = 0;
while (index >= 0 && index < str.length())
{
if (str.at(index) == ' ' || str.at(index) == '\n' || str.at(index) == '\t')
{
return distance;
}
distance++;
index += inc_or_dec;
}
return --distance;
}
MyClass::MyClass()
{
text_edit = new QTextEdit();
QObject::connect(text_edit, &QTextEdit::cursorPositionChanged, this, &MyClass::text_edit_changed_cursor_location);
}
MyClass::~MyClass()
{
delete text_edit;
}
void MyClass::text_edit_changed_cursor_location()
{
QString text_edit_string = text_edit->text();
QTextCursor text_edit_cursor = text_edit->textCursor();
auto current_position = text_edit_cursor.position();
QTextCursor new_text_cursor;
int distance_to_beginning = distance_to_word_beginning_or_end(text_edit_string, current_position, true);
int distance_to_end = distance_to_word_beginning_or_end(text_edit_string, current_position, false);
auto movement_type;
if (distance_to_beginning > distance_to_end)
{
new_text_cursor.setPosition(current_position + distance_to_end);
} else {
new_text_cursor.setPosition(current_position - distance_to_beginning);
}
text_edit->setTextCursor(new_text_cursor);
}

Initializing QVector of QImages

I am fairly new to Qt. It is the first framework I have worked with. I am writing a blackjack game using Qt. It seems to me that I should store the images of each card in a container class such as QVector. The container type would be of QImage. So I would have a declaration such as QVector<QImage> cards; Perhaps this is not the best way about approaching this problem so any alternative suggestion is of course welcomed. However, regardless, I would like to know if it is possible to initialize the container during the declaration. I have not been able to solve this so my solution is the following:
// Deck.h
class Deck
{
public:
Deck();
void shuffle(); // Creates new deck and shuffles it.
QImage &popCard(); // Removes first card off deck.
private:
void emptyDeck(); // Empty the deck so new cards can be added
QVector<QImage> cards;
QQueue<QImage> deck;
};
// Deck.cpp
Deck::Deck()
{
cards.push_back(QImage(":/PlayingCards/Clubs 1.png"));
cards.push_back(QImage(":/PlayingCards/Clubs 2.png"));
cards.push_back(QImage(":/PlayingCards/Clubs 3.png"));
cards.push_back(QImage(":/PlayingCards/Clubs 4.png"));
// continue process for entire deck of cards...
}
This seems to be painfully tedious especially if I consider adding a different style of playing cards later on, or if I give the user an option to change the style of the cards at run time. What would be an efficient design to this?
I would like to know if it is possible to initialize the container during the declaration
Yes you can since C++11:
QList<int> list{1, 2, 3, 4, 5};
Well about your question one of the way can be:
Create in resources all types of your images style calling like template, for example: "Name n.png", where n - number from 1 to 54 (cnt of cards with Jokers);
Create some QList<QImage> (I think it'll be better then QVector);
Create some QMap for searching correct template easy;
Create some enum class for template map;
Write a function that change images of your cards by selected enum.
However it is very light codding. I think there is more usefull ways and there is a lot of other more beauty ways to do this game and logic. But as part of your question here some code (can be not very right, cause write as is):
// Somewhere in global
enum class CardsTemplate: {
Clubs,
SomeTemp1,
SomeTemp2,
...
SomeTempN
}
.H file:
private:
QList<QImage> _images;
QMap<CardsTemplate, QString> _imagesMap {
{CardsTemplate::Clubs, QString("Clubs")},
{CardsTemplate::SomeTemp1, QString("SomeTemp1")},
{CardsTemplate::SomeTemp2, QString("SomeTemp2")},
...
{CardsTemplate::SomeTempN, QString("SomeTempN")}
}
public:
Deck(CardsTemplate temp);
void setNewTemplate(CardsTemplate temp);
.CPP file:
Deck::Deck(CardsTemplate temp){
for(int i = 1; i <= 54; i++)
_images << QImage(QString(":/Playing cards/%1 %2.png")
.arg(_imagesMap.value(temp)).arg(i));
}
void Deck::setNewTemplate(CardsTemplate temp) {
for(int i = 1; i <= _images.size(); i++)
_images[i] = QImage(QString(":/Playing cards/%1 %2.png")
.arg(_imagesMap.value(temp)).arg(i));
}

Error passing pointer in QT

In QT have the following code that starts a thread to send out commands. The thread takes a char * and int as arguments. In the "run" I use the pointer that is given by the constuctor. The code is:
MyThread::MyThread(char * payld, int payld_size)
{
payload_size = payld_size;
payload_p = payld;
}
void MyThread::run()
{
while(...)
{
sendCommand(payload_p, payload_size);
}
}
Unfortunately this doesn´t work and my application crashes when I try to use thread.start(). But when I change it to:
MyThread::MyThread(char * payld, int payld_size)
{
payload_size = payld_size;
payload_p = payld;
for(int i=0; i<payload_size; i++)
{
payload[i] = payld[i];
}
}
void MyThread::run()
{
while(...)
{
sendCommand(payload, payload_size);
}
}
The code does run and only crashes sometimes (looks pretty random to me). Can anybody Explain me why version one doesnt work and version two does? And any ideas on why the second code sometimes crashes? Could it be because the size of payload is not predefined (in the header file I defined it as
char payload[];
When I define it as:
char payload[10];
it seems to work better, but it is annoying to test since the crashes are pretty random.
instead of fiddling with char*, I would switch to QString (since you're using Qt). It takes a bit of learning, but it's almost mandatory to get code working smoothly in this framework. Then declare
QString payload;
and depending on sendCommand implementation, use one of the member functions QString to get the char*, like payload.toLatin1()

C++/CLI How to translate this code ?

I dont know a lot about C++, but I have to make work some C++ code with .NET. I try with DLLImport but I failed. So I try with C++/CLI to make kind of a wrapper.
But I'm not sure to understand everything...
This is the basic C++ H file with the function I want to export (MyFunction)
extern "C"
{
__declspec(dllexport) IplImage* MyFunction(IplImage *src, std::string* name, OneEnumerationType myEnum, bool myBool, float myFloat);
}
This is the Wrapper h code.
#include "MyFunction.h"; // the file containing the h code
#include <string>
namespace MyWrapper{
public ref class MyWrapperClass {
public:
MyWrapper(){};
IplImage^ GetMyFunction(IplImage *src, std::string^ name, OneEnumerationType myEnum, bool myBool, float myFloat);
}
This is the Wrapper cpp code.
#include "MyWrapperCode.h";
namespace MyWrapper{
IplImage^ MyWrapperClass::GetMyFunction(IplImage* src, std:string^ name, OneEnumerationType myEnum, bool myBool, float myFloat){
MyFunction(src, name, myEnum, myBool, myFloat);
}
}
These are my questions :
1) When I'm compiling, the error is "'^ : cannot use this indirection on type IplImage' and same message for type "std::string".
I have followed this logical :
ClasseNative clNat2 = *clNat; --> ClasseManagee clMan2 = *clMan;
ClasseNative &clNat3 = clNat2; --> ClasseManagee %clMan3 = clMan2;
ClasseNative *clNat4 = &clNat2; --> ClasseManagee ^clMan4 = %clMan2;
I have seen, that It was better to use System::String. I try this way but the initial function is using std::string... BTW, why is it better to change ?
2) How do I get the MyFunction IplImage result ? Thru a private member and a get I suppose but I dont know how to initialize it...
3) Tricky question. Is it possible for me to put the CLI obtains IplImage structure (from the OpenCV library) (the result of my function) inside a IplImage .NET structure, when I ll called my wrapper ? Dont know if the question is understandable...
Thanks a lot for your help.
Turning around for 3 days on this problem...
Your wrapper class needs to create a new std::string based on the content of a System::String^ parameter then pass to your native function. Otherwise you need to rewrite the function to take something else as the string input, for example a LPWSTR or LPCSTR.
You can write a ref class to have properties for all data that an IplImage would have, then pass that to your wrapper class. Your wrapper class then create an IplImage object based on the data of the ref class and pass to the native function. Reverse the data copying direction for the return value.
1) just by adding ^ you cannot change a native object to become managed, you have to create wrappers or transfer the data for example:
std::string nativeString = "my string";
String^ managedString = gcnew String(nativeString.c_str());
//now you can return it as
2) create a managed wrapper or use primitive datatype to transfer the data
3) note sure if this will help but look at Emgu.CV
try reading abit more about C++\CLI here are a few nice tutorials:
Quick C++/CLI - Learn C++/CLI in less than 10 minutes
C++/CLI for the C# programmer

Declaring variables in header file of QT causes program to unexpectedly finish

I have a project "Cybaware_ver1" on QtCreator based on QWidget and everything was working fine. I have a header file with a class containing public and private variables and functions. However, since one day whenever I try to declare a new variable in the header file, even an "int" variable, and run the project, I get
The program has unexpectedly finished.
/home/devdeep/Cybaware_ver1-build-Desktop_Qt_5_0_0_GCC_64bit_SDK-Debug/Cybaware_ver1 exited with code 0
Previously I could add variables in the header file without any problem. Currently, I am able to add new functions, modify UI etc. Just I am unable to add variables to the header file. I have found a temporary solution of declaring these variables as static in the cpp file. However, I would like to find a solution to this.
I have tried re-installing Qt but the problem still persists. I am running it on Ubuntu Linux.
Please let me know if there is a way to fix this. Also, I am not sure what other information I can provide. So please let me know about that.
UPDATE: OK. I used the debugger at it says that the error is a segmentation fault.
It points to the following section of qdebug.h and to the line marked by qt_message_output.
public:
inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
inline QDebug(QString *string) : stream(new Stream(string)) {}
inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
inline QDebug &operator=(const QDebug &other);
inline ~QDebug() {
if (!--stream->ref) {
if(stream->message_output) {
QT_TRY {
qt_message_output(stream->type,
stream->context,
stream->buffer);
} QT_CATCH(std::bad_alloc&) { /* We're out of memory - give up. */ }
}
delete stream;
}
}
I guess it is a out of memory error. Possible, closing other programs might help?
UPDATE2: Calling "run qmake" did not help. Here is my header file:
#ifndef MAINVIEW_H
#define MAINVIEW_H
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsLineItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsSimpleTextItem>
#include <list>
#include <QMouseEvent>
#include <QEvent>
#include <QString>
#include <boost/thread.hpp>
#include <qcustomplot.h>
#include <QTimer>
namespace Ui {
class MainView;
}
class MainView : public QWidget
{
Q_OBJECT
public:
int num_missions;
int num_services;
int timer;
int* num_servpermission;
int missionstartx;
//boost::thread pt;
QGraphicsScene mynewscene;
QGraphicsRectItem* myrect;
QGraphicsRectItem** missionline;
QGraphicsLineItem** missionplayline;
QGraphicsRectItem** playrect;
std::list<QGraphicsLineItem**> missionticks;
std::list<QGraphicsEllipseItem**> missionservices;
std::list<QGraphicsSimpleTextItem**> missionservicesno;
std::list<QGraphicsLineItem**>::iterator ti;
std::list<QGraphicsSimpleTextItem**>::iterator ssi;
//std::list<QGraphicsEllipseItem**>::iterator si;
int* missionlength;
int** missionplayxy;
int* missiontickscount;
int* missioniteration;
int missionselected;
int missiontickselected;
int missiontickoffset;
bool isrepeated;
int temp;
QCustomPlot** customPlot;
QTimer dataTimer;
explicit MainView(QWidget *parent = 0);
void playthread();
bool eventFilter(QObject*, QEvent*);
void testfunc();
void clearview();
void test1();
~MainView();
private slots:
void on_startButton_clicked();
void realtimeDataPlot();
void on_pushButton_clicked();
private:
Ui::MainView *ui;
};
#endif // MAINVIEW_H
The program crashes on adding any variable. For example, I added the variable int temp just now and it would crash. Adding anything new makes it crash such as uncommenting the declaration std::list::iterator si;. I take it out and everything works fine.
UPDATE3: My external header file qcustomplot.h is located in the same directory. I am also using boost but that I set up using apt-get. Here is the stack trace:
0 MainView::playthread mainview.cpp 530 0x418fa1
1 boost::_mfi::mf0::operator() mem_fn_template.hpp 49 0x424690
2 boost::_bi::list1 >::operator(), boost::_bi::list0> bind.hpp 253 0x424600
3 boost::_bi::bind_t, boost::_bi::list1 > >::operator() bind_template.hpp 20 0x4245af
4 boost::detail::thread_data, boost::_bi::list1 > > >::run thread.hpp 61 0x42431c
5 thread_proxy /usr/lib/libboost_thread.so.1.46.1 0x7ffff7bcfba9
6 start_thread /lib/x86_64-linux-gnu/libpthread.so.0 0x7ffff6557efc
7 clone /lib/x86_64-linux-gnu/libc.so.6 0x7ffff5af159d
8 ??
The 0th pt in the above trace points to
ui->missionview->invalidateScene()
Here the graphics view gets updated from a function playthread binded to a boost thread. But I am not sure why this would be a problem on adding a variable in the header file as I am not using the variable anywhere in the cpp file.
Go into the project folder and delete the *.pro.user file. Next time you open the project you'll just need to reconfigure it (i.e. choose the appropriate build kit).
I know this is an old thread, but I ran into the same problem myself. Every time I tried to add a variable to my header file it was causing a heap corruption error. I wasn't allocating memory on the heap, or even declaring pointer variables for that matter. It was very frustrating because I didn't know what it was at first. I finally narrowed it down to adding variables to my header file causing the crash. Based on doomguy's experience I knew that it was something persistent in the build folder because he said reinstalling Qt didn't work. I'm glad I read that because I would have been pissed if I reinstalled this behemoth and it still didn't work. Based on prior experience corruption in the .user file can cause issues, and deleting it kinda gives you a clean slate. Moral of the story: If you're getting unusual behavior that seems to point to a problem in the IDE try deleting the .user file before trying more extreme measures.
Not sure if the problem is cause by this, but if the header contains a QObject derived class you should run qmake and after that build your application.
LE: also if this isn't the case, i suggest that you show us a little more code (your code, not Qt code)

Resources