gpt4 book ai didi

c++ - 调用子类 QWidget 的方法

转载 作者:行者123 更新时间:2023-11-28 00:57:08 24 4
gpt4 key购买 nike

它应该很容易,但有些东西不起作用,我找不到问题。

我有两个类,如下所示:

1) 个人标签

#ifndef INDIVIDUALTAB_H
#define INDIVIDUALTAB_H

#include <QMainWindow>
#include <QInputDialog>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QLabel>

class IndividualTab : public QWidget
{
Q_OBJECT

public:
IndividualTab(QWidget *parent = 0);
QTableWidget *table;
QVBoxLayout *layout;

};
#endif // INDIVIDUALTAB_H


IndividualTab::IndividualTab(QWidget *parent) : QWidget()
{
table = new QTableWidget(0,4);
layout = new QVBoxLayout();
}

2)二等舱

#ifndef SECONDCLASS_H
#define SECONDCLASS_H

#include "individualtab.h"

class secondClass : public QMainWindow
{
Q_OBJECT

public:
explicit secondClass(QWidget *parent = 0);
Ui::secondClass *ui;
~secondClass();

QList<IndividualTab> *individualTabList;
};
#endif // SECONDCLASS_H


secondClass::secondClass(QWidget *parent) : QMainWindow(parent), ui(new Ui::secondClass)
{
ui->setupUi(this);
}


secondClass::~secondClass()
{
delete ui;
}

void secondClass::addNewItem()
{
//Getting parameters
QList<QString> parameters;
//creating QList

//Updating individualTab
for(int i = 0; i < ui->tabWidget->count(); i++)
{
if(parameters.at(0) == ui->tabWidget->tabText(i))
{
IndividualTab tab = individualTabList->at(i);
tab.addItem(parameters);
break;
}
}
}

当我编译时出现这个错误:

In file included from ../secondclass.h:5:0,
from ../secondclass.cpp:1:
/usr/include/qt4/QtGui/qwidget.h: In copy constructor 'IndividualTab::IndividualTab(const IndividualTab&)':
/usr/include/qt4/QtGui/qwidget.h:806:5: error: 'QWidget::QWidget(const QWidget&)' is private
../individualtab.h:10:7: error: within this context
../secondclass.cpp: In member function 'void SecondClass::addNewItem()':
../secondclass.cpp:142:56: note: synthesized method 'IndividualTab::IndividualTab(const IndividualTab&)' first required here

我认为问题出在 IndividualTab tab = individualTabList->at(i);所以我改变了

IndividualTab *tab = new IndividualTab();
tab = individualTabList->at(i);

但在这种情况下,我遇到了这个错误:

../secondclass.cpp: In member function 'void SecondClass::addNewItem()':
../secondclass.cpp:143:42: error: cannot convert 'const IndividualTab' to 'IndividualTab*' in assignment
../secondclass.cpp:144:17: error: request for member 'addItem' in 'tab', which is of non-class type 'IndividualTab*'
In file included from ../secondclass.h:5:0,
from ../secondclass.cpp:1:
/usr/include/qt4/QtGui/qwidget.h: In copy constructor 'IndividualTab::IndividualTab(const IndividualTab&)':
../individualtab.h:10:7: instantiated from 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = IndividualTab]'
/usr/include/qt4/QtCore/qlist.h:512:13: instantiated from 'void QList<T>::append(const T&) [with T = IndividualTab]'
../Ripetizioni/secondclass.cpp:112:38: instantiated from here
/usr/include/qt4/QtGui/qwidget.h:806:5: error: 'QWidget::QWidget(const QWidget&)' is private
../individualtab.h:10:7: error: within this context
In file included from /usr/include/qt4/QtCore/qobject.h:50:0,
from /usr/include/qt4/QtGui/qwidget.h:46,
from /usr/include/qt4/QtGui/qmainwindow.h:45,
from /usr/include/qt4/QtGui/QMainWindow:1,
from ../secondclass.h:4,
from ../secondclass.cpp:1:
/usr/include/qt4/QtCore/qlist.h: In member function 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = IndividualTab]':
/usr/include/qt4/QtCore/qlist.h:372:58: note: synthesized method 'IndividualTab::IndividualTab(const IndividualTab&)' first required here

提前致谢!

最佳答案

QWidget 不应该被复制。您的 individualTabList 应该是指向 IndividualTab 的指针的容器。并且没有充分的理由让该成员本身成为指针。将声明更改为:

 QList<IndividualTab*> individualTabList;

然后你可以:

IndividualTab *tab = individualTabList.at(i);

关于c++ - 调用子类 QWidget 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10469472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com