简体   繁体   中英

Qt-help creating Qt Designer custom widget plugin like QTabWidget

I have created a Qt Designer custom widget plugin that contains a QListView and a QStackedWidget . When I installed the plugin in the Qt creator, I can't access the QListView 's items by click, even if I connect the clicked signal with a slot. My problem is, I need the slot to execute in the Qt Designer, because i want the widget to behave just like the QTabWidget , but insted of tab, I want to use list to navigate between the pages. But in my situaton, when I click on one of the items, the complete widget gets the click, and not just the item in the list, so i wan't to know if there is any possiblity to make that possible. I hope i explained my point well and thnx.

The constructor of the custom widget class:

MultiPageWidget::MultiPageWidget(QWidget *parent)
: QWidget(parent)
, stackWidget(new QStackedWidget)
, listView (new QListView)
, listModel (new ListModel)
{

    listView->setObjectName(QStringLiteral("__qt__passive_listView"));
    listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    listView->setModel(listModel);
    
    connect(listView, SIGNAL(clicked(QModelIndex)), this, SLOT(setCurrentIndex(QModelIndex)));
    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(listView,1);
    layout->addWidget(stackWidget,2);
    //listView->setFocus();

}

the domXml() methode int plugin's class:

QString MultiPageWidgetPlugin::domXml() const
{

    return QLatin1String("
    <ui language="c++">
    <widget class="MultiPageWidget" name="multipagewidget">
    <widget class="QWidget" name="page" />
    </widget>
    <customwidgets>
    <customwidget>
    <class>MultiPageWidget</class>
    <extends>QWidget</extends>
    <addpagemethod>addPage</addpagemethod>
    </customwidget>
    </customwidgets>
    </ui>"
    );

}

在此处输入图像描述

Note that I wanted to add this as a comment first since it is not a fully answer, but my reputation is too low.

Connection in the QT Designer:

The clicked() signal is emitted when the application is running not in the Qt Designer. I think what is missing here is the connection of the QAbstractItemView::currentChanged signal of your QListView and the currentIndexChanged slot of your MultiPageWidgetPlugin. This is shown for example in the Qt documentation ( https://doc.qt.io/qt-5/qtdesigner-containerextension-example.html ):

QWidget *MultiPageWidgetPlugin::createWidget(QWidget *parent)
{
    MultiPageWidget *widget = new MultiPageWidget(parent);
    connect(widget, &MultiPageWidget::currentIndexChanged,
            this, &MultiPageWidgetPlugin::currentIndexChanged);
    return widget;
} 

So what you need is to emit a signal in your MultiPageWidget class, you can name it currentIndexChanged as in the Qt documentation for example. This signal has to be emitted when the currentIndex of your ListView change, so as I above suggested I would use the QAbstractItemView::currentChanged signal. You also have to find a conversion between the emitted QModelIndex parameter in the QAbstractItemView::currentChanged signal and the integer parameter in the currentIndexChanged signal of the MultiPageWidgetPlugin.

I hope this was helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM