调用子类化QWidget的方法

时间:2012-05-06 09:23:48

标签: c++ class qt4

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

我有两个课程,如下所示:

1)individualtab

#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)secondclass

#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 

提前感谢!

1 个答案:

答案 0 :(得分:3)

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

 QList<IndividualTab*> individualTabList;

然后你可以:

IndividualTab *tab = individualTabList.at(i);