2017-09-26 15 views
0

QWidgetとUi_Formから継承したクラスを持っています(Qtで.uiを作成すると自動生成されたクラスが表示されます)。QWidget "access violation" exeption

class MyClass: public QWidget, public Ui_Form {} 

Ui_Formは.uiファイルの関連ウィジェット(例えば、QLineEdits、QButtons、など)と接続され、いくつかのメンバーが、持っているように見えます。

class Ui_Form { 
public: 
QLineEdit *fileNameEdit; 

    void setupUi(QWidget *Form) { 
    fileNameEdit = new QLineEdit(layoutWidget); 
    fileNameEdit->setObjectName(QStringLiteral("fileNameEdit")); 
    } 
} 

MyClassはUi_Formから継承されているため、このmembesを使用できます。しかし、私が何かしようとすると、「アクセス違反の読書場所」という例外があります。例:

fileNameEdit->setText("String"); 

誰かアドバイスできますか?

+0

setupUiを実行した後でやりますか? – dbrank0

+0

[ドキュメンテーション](http://doc.qt.io/qt-4.8/designer-using-a-ui-file.html)は、コンストラクタで 'setupUi(this) 'を呼び出す必要があることを示しています。あなたは? – Botje

+0

はい、私はsetupUIを実行しました。メンバーはヌルと等しくない。しかし、間違いはとにかく現れる – Dmitrii

答えて

1

Ui_Form部分を組み込む方法は、デフォルトではQt proposesではありません。あなたはこのbutton exampleに見た場合は、UI部品がdiferently組み込まれているかを確認することができます

ヘッダファイル

#ifndef BUTTON_H 
#define BUTTON_H 

#include <QWidget> 

namespace Ui { 
class Button; 
} 

class Button : public QWidget 
{ 
    Q_OBJECT 

public: 
    explicit Button(int n, QWidget *parent = 0); 
    ~Button(); 

private slots: 
    void removeRequested(); 

signals: 
    void remove(Button* button); 
private: 
    Ui::Button *ui; 
}; 

#endif // BUTTON_H 

CPPコード

#include "button.h" 
#include "ui_button.h" 

Button::Button(int n, QWidget *parent) : 
    QWidget(parent), 
    ui(new Ui::Button) 
{ 
    ui->setupUi(this); 
    ui->pushButton->setText("Remove button "+QString::number(n)); 
    addAction(ui->actionRemove); 
    connect(ui->actionRemove,SIGNAL(triggered()),this,SLOT(removeRequested())); 
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(removeRequested())); 
} 

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

void Button::removeRequested() 
{ 
    emit remove(this); 
} 

は、主な違いは、私は信じているということですあなたはUi_From::setupUi関数を呼び出していません。 Qt提案のテンプレート(それを継承するのではなく、クラスメンバとして組み込む)に従う必要はありませんが、Qtの提案に従えば私の立場からははるかに明確です。

+1

実際には、複数の継承による 'Ui_Form'を含むことは最良の解決策ではありません。MyClassのユーザーの依存関係を高め、UIのメンバーを公開します。代わりに保護された継承によってこれを避ける)。 – cbuchart

関連する問題