PDA

View Full Version : Attempting to set QLayout on ..., which already has layout



Pawello
17th August 2012, 14:15
Hi, im trying to make program including simple version of model from Basic Sort/File Model Example (http://doc.qt.nokia.com/4.7-snapshot/itemviews-basicsortfiltermodel.html) , but I get an empty window and warning :
QWidget::setLayout: Attempting to set QLayout "" on EGlowny "", which already has layout

I've checked code few times and I don't see where I did this.

Here you have fragment of my code:

main.cpp


#include <QApplication>
#include "eglowny.h"
extern QAbstractItemModel *asd(QObject *parent);
extern void wbaze();
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
EGlowny okno;
okno.setSourceModel(asd(&okno));
okno.show();
return app.exec();
}

Eglowny.h


#ifndef EGLOWNY_H
#define EGLOWNY_H
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QFileDialog>
#include <QMessageBox>
#include <QAbstractItemModel>
#include <QTreeView>
#include <QSortFilterProxyModel>
#include <QComboBox>
#include <QGridLayout>
extern void wbaze(QString &nazwapliku);
extern void zapiszbaze(QString &nazwapliku);
class ToolBar;
QT_FORWARD_DECLARE_CLASS(QMenu)
QT_FORWARD_DECLARE_CLASS(QSignalMapper)
class EGlowny : public QMainWindow
{
Q_OBJECT

public:
explicit EGlowny(QWidget *parent = 0);
void setSourceModel(QAbstractItemModel *model);
private:
void pasekmenu();
void wnetrzeokna();
QSortFilterProxyModel *proxyModel;
QGridLayout *proxyLayout;
QTreeView *proxyView;
QLabel *filterPatternLabel;
QLabel *filterColumnLabel;
QLineEdit *filterPatternLineEdit;
QComboBox *filterColumnComboBox;
private slots:

void filterREChanged();
void filterColumnChanged();
};
#endif // EGLOWNY_H




Eglowny.cpp


#include "eglowny.h"

EGlowny::EGlowny(QWidget *parent) :
QMainWindow(parent)
{
pasekmenu();
wnetrzeokna();
setWindowTitle("Program");
resize(800, 600);
}

void EGlowny::wnetrzeokna()
{
proxyModel = new QSortFilterProxyModel;
proxyModel->setDynamicSortFilter(true);

proxyView = new QTreeView;
proxyView->setRootIsDecorated(false);
proxyView->setAlternatingRowColors(true);
proxyView->setModel(proxyModel);
proxyView->setSortingEnabled(true);

filterPatternLineEdit = new QLineEdit;
filterPatternLabel = new QLabel(tr("Filter Pattern: "));

filterColumnComboBox = new QComboBox;
filterColumnComboBox->addItem(tr("1"));
filterColumnComboBox->addItem(tr("2"));
filterColumnComboBox->addItem(tr("3"));
filterColumnLabel = new QLabel(tr("Filter column: "));

connect (filterPatternLineEdit, SIGNAL(textChanged(QString)),this,SLOT(filterRECha nged()));
connect (filterColumnComboBox, SIGNAL(currentIndexChanged(int)),this,SLOT(filterC olumnChanged()));

proxyLayout = new QGridLayout;
proxyLayout->addWidget(proxyView, 0, 0, 1, 3);
proxyLayout->addWidget(filterPatternLabel, 1, 0);
proxyLayout->addWidget(filterPatternLineEdit, 1, 1, 1, 2);
proxyLayout->addWidget(filterColumnLabel, 2, 0);
proxyLayout->addWidget(filterColumnComboBox, 2, 1, 1, 2);
setLayout(proxyLayout);

proxyView->sortByColumn(1,Qt::AscendingOrder);
filterColumnComboBox->setCurrentIndex(1);
}
void EGlowny::pasekmenu()
{
QMenu *Plik = menuBar()->addMenu(tr("Baza"));
QAction *action = Plik->addAction(tr("..."));
}

baz.h - from this file comes extern functions


QAbstractItemModel *asd(QObject *parent)
{
QStandardItemModel *model = new QStandardItemModel(0, 3, parent);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Subject"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Sender"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Date"));
dodajwpis(model, "as", "qb", "sac","wd");
dodajwpis(model, "aa", "bs", "sc","asd");
dodajwpis(model, "xw", "yx", "sz","saa");
return model;
}

void dodajwpis(QAbstractItemModel *model, QString wpistyp, QString wpisnazwa, QString wpiszaw, QString wpiskraj)
{
model->insertRow(0);
model->setData(model->index(0,0),wpistyp);
model->setData(model->index(0,1),wpisnazwa);
model->setData(model->index(0,2),wpiszaw);
model->setData(model->index(0,3),wpiskraj);
}

maximebd
17th August 2012, 15:39
EGlowny derives from QMainWindow which already has a layout with a center widget, a dockable toolbar and menubar. The code you provided didn't compile, but what you need to do is to set up your layout on the centralWidget() instead of the main window. You could also derive EGlowny from a QWidget instead of a QMainWindow if you don't need the facilities of the QMainWindow.

Pawello
18th August 2012, 08:57
Thank you. Problem solved