它应该真的很容易,但是有些东西行不通,我找不到问题。

我有两节课,如下所示:

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);

09-19 05:21