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++
Related
In JavaFx, I understand that if I want a button to make some code run when it is clicked, I need to somehow have the code that I want to have run inside a method, and because this is Java, I wrap that method inside a class that extends EventHandler . For example:
// (myButton is a reference variable to a Button object)
myButton.setOnAction(new MyButtonEventHandlerClass() );
// inner class
public class MyButtonEventHandlerClass extends EventHandler<ActionEvent>{
public void handle(ActionEvent e) {
// (some code)
}
}
My confusion is: why is JavaFX designed to require me to make an instance of the class holding the handle() method? I had thought that non-static methods are used when the instance variables of an object are used; or in other words, if you just need a method that does not need an object, then you should use a static method. In this kind of thinking, handle() sounds like it should be a static method.
Why is handle() not a static method?
The criteria for a EventHandler to work in a meaningful way in this case are:
There needs to be some way to store the information.
The information has to be stored in a way that allows more than one way of dealing with a event.
Now regardless of the handle method actually using any fields in the EventHandlerand/or enclosing classes, there needs to be a way do identify the code that should handle the event.
If handle only was a static method, there only would ever be a single handler which even worse would be determined by the JavaFX programmers, since static methods cannot be overridden. It would not be possible to fulfil condition 2. without a non-static method.
For non-static methods however it's pretty simple to deal with this. Methods can be overridden and handling the event the correct way can simply be done by invoking EventHandler.handle for the event handler object.
In java 8 however method references (or lambda expressions) could be used to shorten this a bit by using method references, which allows you to "use a method as interface instance":
public class MyClass {
public static void handleMyButtonAction(ActionEvent evt) {
// (some code)
}
}
myButton.setOnAction(MyClass::handleMyButtonAction);
I have been making my C++ functions callable from Qml by following the approach given in Qt documentation.
This require one of these conditions to be fulfilled :
Make the C++ function public and Q_INVOKABLE
or
Make the C++ function a public slot
This sometimes is not in sync with my class design. As in, the function which I want to be callable from Qml, is private according to my class design.
Q1. Can I make a function visible to Qml and still keep it private ?
Q2. Is it my design flaw to expect this kind of behavior ?
Well, if you do something private by design you consider that it's something to be used only within a class. Now you are now asking actually is how can I workaround my design. Obvious answer is - you can make a public wrapper in a class which will invoke your private method and publish this public wrapper into QML, but I would suggest to review design if you face such situation.
Implementing a derived “QAbstractListModel::data” method.
Q_DECLARE_METATYPE(myType); doesn’t even compile…. returning my custom object results in a compilation error.
How can this be done?
QVariant::fromValue<QObject *>(object);
replace QObject with your own type, but use Q_DECLARE_METATYPE on it. Keep in mind what you are declaring: the MyType or MyType *. Since you are talking about passing an object from QAbstractItemModel::data, then I suppose that you want to provide a pointer to the object, is this correct? If so, then declare it like this:
typedef MyType * MyTypeStar
Q_DECLARE_METATYPE(MyTypeStar);
This will make MyType * known to the meta-type system.
If you want to pass around the object itself, then declare the way you tried, but make sure that you properly define your type:
Q_DECLARE_METATYPE: This macro makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the type Type as a custom type in QVariant.
Why wont put it inside QVariant as QObject ? qVariantFromValue(youObjectPointer)
Can somebody please explain why do I create in qt public slots but not public signals?
Slots are normal functions, and can be public, private or protected.
Signals are always protected when eventually generated by the 'moc' program.
But note this (from some old Qt 4.7 docs):
Since slots are normal member functions, they follow the normal C++ rules when called directly. However, as slots, they can be invoked by any component, regardless of its access level, via a signal-slot connection. This means that a signal emitted from an instance of an arbitrary class can cause a private slot to be invoked in an instance of an unrelated class.
SLOTS are functions which can be public, private or protected.Functions are called from any where i.e with in the class or outside the class.But SIGNALSare like events and it should be emitted within the class or from the inherited class so SIGNALSare always protected.
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