1

I am using a QWidget in which I would like to put some separator lines. As separator lines I am using this

QFrame *seperatorLine = new QFrame(_toolBar);
seperatorLine->setFrameStyle(QFrame::Sunken | QFrame::VLine);

I need several separator lines and I was curious whether I need to create a new QFrame every time or whether there is a way to reuse one (or use a copy-constructor).

At the moment the line is only at the last position I added it to the QWidget.

liquid.pizza
  • 505
  • 1
  • 10
  • 26
  • 3
    I believe each GUI element should be represented by a separate instance of `QFrame` (QWidget), so you need several separator lines. – vahancho Jul 08 '14 at 08:37

1 Answers1

3

QObject and thus QWidget derived class cannot access a copy constructor.

Instead of that, you can encapuslate your QFrame property in a little factory method:

QFrame* createSeparator(QWidget* parent=0) {
    QFrame *separatorLine = new QFrame(parent);
    separatorLine->setFrameStyle(QFrame::Sunken | QFrame::VLine);
    return separatorLine;
}

I prefer this method over subclassing QFrame to tweak several properties of a QFrame instance

A fancy way to "clone" a QObject would be to create a new object and assign all declared properties. Of course it is only useful if you want to transfer values:

CustomObject* CustomObject::clone() {
    int count = metaObject()->propertyCount();
    CustomObject* clone = new CustomObject(this->parent());

    for (int i = 0; i < count; i++) {
        const char* prop = metaObject()->property(i).name();
        clone->setProperty(prop, property(prop));
    }
    return clone;
}
epsilon
  • 2,849
  • 16
  • 23
  • thanks, this works. I thought about this solution, but wanted to know if there is any fancy Qt-way. I mistyped `separatorLine`. You may want to change it in your answer. – liquid.pizza Jul 08 '14 at 08:50