2015-02-24 69 views
1

我想显示一个非常简单和短名单的国家使用QStringListModelQListView。但是当我编译它时,我会看到一个空白的窗口。QListView不能在Mac OS中显示

这是我的类代码:

#include "FenPrincipale.h" 

FenPrincipale::FenPrincipale() 
{ 
    QVBoxLayout *layout = new QVBoxLayout; 

    QStringList listePays; 
    listePays << "France" << "Espagne" << "Italie" << "Portugal" << "Suisse"; 
    QStringListModel *modele = new QStringListModel(listePays); 

    QListView *vue = new QListView; 
    vue->setModel(modele); 
    layout->addWidget(vue); 

    setLayout(layout); 
} 

标题:

#ifndef FENPRINCIPALE_H 
#define FENPRINCIPALE_H 

#include <QMainWindow> 
#include <QVBoxLayout> 
#include <QStringList> 
#include <QListView> 
#include <QStringListModel> 

class FenPrincipale : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    FenPrincipale(); 
}; 

#endif // FENPRINCIPALE_H 

主要:

#include "FenPrincipale.h" 
#include <QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    FenPrincipale w; 
    w.show(); 

    return a.exec(); 
} 

pro文件:

QT  += core gui 

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 

TARGET = utilisationModeleSimple 
TEMPLATE = app 


SOURCES += main.cpp\ 
     FenPrincipale.cpp 

HEADERS += FenPrincipale.h 

FORMS += FenPrincipale.ui 

我目前使用基于Qt 5.4.0(Clang 6.0(Apple),64位)的Qt Creator 3.3.0; OS X Yosemite 10.10.2

这段代码有什么问题?

回答

2

如果你想使用QMainWindow,那么你需要设置你的部件为中心:QMainWindow::​setCentralWidget。 Qt文档中有很好的示例。

FenPrincipale::FenPrincipale() 
{ 
    QStringList listePays; 
    listePays << "France" << "Espagne" << "Italie" << "Portugal" << "Suisse"; 
    QStringListModel *modele = new QStringListModel(listePays); 

    QListView *vue = new QListView; 
    vue->setModel(modele); 

    setCentralWidget(vue); 
} 

如果您不需要QMainWindow功能,那么只需interhit QWidget,而不是QMainWindow - 和你的代码将工作。