Is it possible to make QList<QAbstractListModel>? - c++

I have a custom C++ class, called CPage, which inherits of QAbstractListModel.
class CPage : public QAbstractListModel
In another class called CSession, I want to have a set of CPage. For this purpose, CSession have a private attribute called Q_PageSet:
QList<CPage*> Q_PageSet;
My problem is: when I try to do the following, my program crashes:
Q_PageSet.size() or Q_PageSet.count()
So my question is: Is that because I am not allowed to make a QList of QAbstractListModel ?
Thanks.

Related

Subclass of QAbstractListModel only available as superclass in QML

I have a subclass of QAbstractListModel called ObjectListModel which contains a method remove():
class ObjectListModel : public QAbstractListModel
{
public:
Q_INVOKABLE void remove(int index);
}
However, when I try to call remove() from within QML, I get the following error:
TypeError: Property 'remove' of object QAbstractListModel(0x13c3c0) is not a function
I expose an instance of ObjectListModel through a property of MyApplication:
Q_PROPERTY(ObjectListModel *uploadModel
MEMBER _uploadModel
NOTIFY uploadModelChanged)
and I register the type as follows in main():
qRegisterMetaType<ObjectListModel*>("ObjectListModel*");
Any idea what is happening here?
You are missing the Q_OBJECT macro.
Also, you are registering incorrectly, you need something like:
qmlRegisterType<List>("ModuleName", 1, 0, "ObjectListModel");
QObjects are implicitly meta types, since they get the moc treatment (if you don't forget the macro). So there is no need to registering them as meta types.
The Q_OBJECT macro is missing.

Using Interface class in form, in order to use the derived class desired

I would like to use different custom widgets on the same area (that depends on the situation). For that I created an Interface class and some derived custom classes widgets (because they have same methods and for the cleanliness).
My Interface is :
IDial
Derived Classes :
FirstDial, SecondDial
These derived classes inherit from IDial, so they have common functions from IDial.
When I start my program, I would like to chose which dial I will display, it depends of macros or parameters (it's not important).
In order to be able to display the derived class (widget) that I want, I have no other choices than put the Interface class name (IDial) as "objectName" of my widget area in the form (design mode).
The problem is that Qt is trying to instantiate this Interface... (it's impossible and normal because of pure virtual functions).
I would like to indicate that the area can contain different widgets, which all inherit from this Interface.
Instead of class IDial, add QFrame to the place where you want. In your header file:
#include "firstdial.h"
#include <QHBoxLayout>
...
QHBoxLayout* layout;
FirstDial* firstDial;
In source file create new layout and object of your class:
ui->frame->setFrameShape(QFrame::NoFrame); // a frame you've created
layout = new QHBoxLayout(ui->frame);
firstDial = new FirstDial;
Add your widget to layout:
layout->addWidget(firstDial);

Creating a class derived from CTabCtrl

I am following a tutorial to use CTabCtrl. The tutorial is asking me to create a class that is derived from CTabCtrl. I am unsure if I am doing this correctly. This is the header for my custom class.
#pragma once
// CTabbyControl
class CTabbyControl : public CTabCtrl
{
DECLARE_DYNAMIC(CTabbyControl)
public:
CTabbyControl();
virtual ~CTabbyControl();
protected:
DECLARE_MESSAGE_MAP()
};
I am using Visual Studio 2010 and am following this tutorial.
My problem is that the tutorial asks me to create a CTabbyControl variable in the class wizard. I only have the option to use CTabCtrl. This brings me to think that I am deriving incorrectly. I have never derived a class. Is the way I am doing it a correct method of deriving?
http://simplesamples.info/MFC/CTabCtrl.html
Please check whether you have included the Header file (TabbyControl.h) in the class you try to create the object of TabbyControl.

Qt Extending my own widget

To put it simply, I want a new class that extends a custom widget that I've made, and thus have full access to it's UI.
I've tried several different methods so far based on how one would normally subclass/extend classes, but I'm somehow failing horribly for so many different reasons.
Additionally, simply using my widget as a member in my new class wouldn't do for this situation.
Can someone illustrate a quick example of how I would do this? I have done a bunch of searching but I can't seem to find any hits relating to exactly what I'm trying to do
If all else fails I will simply copy over the code and make an actual new widget, which technically would have saved me lots time, but it just doesn't feel right doing that.
My first instinct was to do something like this ( Qwe being my new class, Asd being the widget ):
class Qwe : Asd {public: ...}
And I even made the widget's Ui public, but then I just got the error :
use of undefine type Ui::Asd
whenever I tried to access the Ui's elements.
Let's say we have a custom widget named BaseWidget and a child widget named ChildWidget. Declare BaseWidget as usually, but make its ui member protected instead of private, like this:
protected:
Ui::BaseWidget *ui;
Declare ChildWidget as an ordinary widget derived from BaseWidget. Make sure you include ui_BaseWidget.h in the ChildWidget.cpp file, just as you do it in BaseWidget.cpp (this include and the header itself is generated by Qt).
Header:
#include "BaseWidget.h"
class ChildWidget : public BaseWidget {
Q_OBJECT
public:
explicit ChildWidget(QString text, QWidget *parent = 0);
};
Source:
#include "ChildWidget.h"
#include "ui_BaseWidget.h"
ChildWidget::ChildWidget(QString text, QWidget *parent) :
BaseWidget(parent)
{
ui->label->setText(text);
}

How to set user data for a QWidget?

I would like to set an integer number to be stored in a QWidget, and I think the setUserData member function would do the trick but I can't find any documentation. Any hints?
You might be looking for QObject::setProperty() (which is of course inherited by QWidget).
I am not an expert in QT but why not just create a class that inherits from QWidget and has an integer? Like so:
class MyDerivedWidget : public QWidget
{
public:
MyDerivedWidget();
private:
Data *myUserData;
};
Or if you insist on using the setUserData checkout the last post here.