How to set properties to an object in a scene in Qt - qt

I have created an Scene in Qt using QGraphicsScene. And I have placed many objects in the scene like car , bus , etc using QGraphicsPixmapItem. Now I want to assign some properties to
these objects that I placed so that i can make them run. How do I do that . For example I want to associate each object with my custom class objects . Can anyone help me . I also noticed setData function which associate key to value . Can this be used if so how . What's the standard way of doing this.

If its possible to and you want to use Qt's property system, inherit from QObject as well as the QGraphicsItem. Make sure to also define the Q_OBJECT macro.
class CustomPixmapItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
...

Related

How design a QWidget?

how should the member of a QWidget-Class normally designed private or public?
class MyWidget : public QWidget {
public:
MyWidget( QWidget *parent = 0);
QLineEdit *myLine;
}
or
class MyWidget : public QWidget {
public:
MyWidget( QWidget *parent = 0);
private:
QLineEdit *myLine;
}
I prefer the second definition with the private member, but this version is bad to test because I have no access to the member with the findChild( QString ).
Other part is should a QWidget class nested other widgets? Problem is there is no direct access to the nested widgets and this isn't really usefull for automated gui tests where the mouse click should mabyee push a button or something else..
The second choice is generally regarded as the "best programming practice", and if you need to access the private members via code from somewhere else (including your test suite), then you need to implement getters and setters.
Generally, test frameworks should also be using APIs to access objects rather than directly accessing members, for it is that API that you typically want to test within unit tests. If you really can't give up the notion that the test framework should directly access the private members, then you can look into using C++ friend classes. But you didn't hear that from me.
If you need to access something generically (like by a string name), that's exactly what Qt Properties was designed to do for you. So declare your items as a property and list the getters/setters/signals/etc that way too.
Aside: Unfortunately, implementing getters and setters is somewhat boring work, which is why I ended up implementing a QtCreator plugin tool that implements getters, setters and creates signal definitions and emits the signal. If you ever need to actually do something more complex in the future, then simply remove the and replace the auto-getter/setter with your more complex definitions.

Is it possible to move a object that was created dynamically into a qthread?

It's a general question about qthreads, I know how to use the movethread() function but that was for object that was not created dynamically.
The user will create the object on a GUI application by selecting add button. A remove button will also be available if the user would like to remove that object that was created. The user should be able to select the object from the listview. I've already created examples of a custom listview using qstyleditemdelegate, I know how to move the list into a model. I'm worried about the threads.
What I would like to do is allow the user to create an object of a certain class dynamically. Each object with then be moved into qthread. Should I keep looking? Any tips, tricks or hints?
Can I create a function that moves the object into a thread?
Pseudo Code:
void MoveThisObject(MyCustomObject Object)
{
QThread* thread = new QThread;
Object->moveToThread(thread);
/****setup connections****/
thread->start();
}
I'm still writing down what I'll need, I haven't really created the actual application.
You can move a QObject (as long as it's not a QWidget, of course) to a different thread regardless of whether it was created dynamically or not. Most QObjects in most applications are created dynamically anyway, or else it would be difficult for them to be polymorphic.

error: 'QDialog::QDialog(const QDialog&)' is private

As the title stands I get this error within content of this snippet:
class NewTaskDialog : public QDialog
It was working just fine earlier, but error started showing up when I added method:
void MainWindow::saveButtonClicked(NewTaskDialog dialogWindow)
Your syntax for saveButtonClicked creates a copy of the NewTaskDialog that's passed to it. You can't copy QWidgets unless you create a cloning function that explicitly provides the exact functionality you seek. QWidget's constructor is private.
You must pass a pointer
void MainWindow::saveButtonClicked(NewTaskDialog* dialogWindow)
or a reference. Using the pointer is the standard Qt way.
Use a pointer to the QDialog instead. The QDialog class has the copy constructor defined as private to try prevent you from passing a QDialog by value since you should never do that.
What's the use of the private copy constructor in c++

Qt: bring base class method into slots in derived class

I have a class which derives from a QWidget and a model class. Based on my reading, I can't have the model class to derive from QObject.
class PageWidget : public QWidget,
public MyModelClass
{
...
};
the model class MyModelClass already have method to set properties such as setWidth(bool). It seems that I can't use those method directly as slots. If I declare:
QObject::connect(button, SIGNAl(dataChanged(bool)), this, SLOT(setWidth(bool)));
Qt complains at runtime that no slot setWidth(bool). I have to manually add each method into PageWidget, which simply calls the same method of MyModelClass.
Is there any way to bring base methods into slots without redeclaring every method?
QMetaObject (the Qt part that allow you to use slots) and multiple inheritance do not mix. You solution of creating "pass through" slots is a way of solving it. However, if you do not need to do the multiple inheritance, I wouldn't do it. Just from the look of things, it seems weird for a PageWidget to derive from both a Widget and a Model. It probably makes more sense to have it contain the model instead.

Expression Blend does not list my application's objects in CLR objects

I'm following a video tutorial on data binding with Visual Studio / Expression Blend.
In the tutorial the application's custom objects are listed when the presenter clicks on the "+CLR Object" button, but in when I do it, my application's objects are not listed.
What do I need to do to get my application's objects to be listed here?
Do you have a reference between the projects? Seems like the child project is just missing a reference to the parent so they can be picked up.
You also need to make sure that if you are using parameterised constructors that your object also has a default constructor - this problem drove me a bit mad until I realised this.
public class MyThing{
private int _item;
//If this is the only constructor Expression does not show it up
public MyThing(int item){
_item = item;
}
//Expression will only list your object if you add this constructor
//when you also have parameterised constructors
public MyThing(){}
}
I had the same problem. I did not make the classes in my C# code public.
I had this:
class MyClass
needed this:
public class MyClass

Resources