PDA

View Full Version : QMdiArea / SubWindow Problem



ericV
18th September 2009, 14:09
Hi,

I am currently working on an MDIbased Project.

I ahve set up an MDI area, and am working on adding different widgets to it.

But i am having problems with one of my Widgets. It is a custom codeeditor widget subclassed from QFrame, that contains a QTextEdit and a QWidget in which i draw the linenumbers.

"My Editor Constructor"

TextEditorWidget::TextEditorWidget(QWidget *parent,const QString &file)
: QFrame(parent)
{
setFrameStyle(QFrame::StyledPanel|QFrame::Sunken);
setMidLineWidth(2);

editor = new QTextEdit(this);
editor->setAcceptRichText(true);
...
lineNumbers = new LineNumbers(this);

QHBoxLayout *editorLayout = new QHBoxLayout;
editorLayout->setSpacing(0);
editorLayout->addWidget(lineNumbers);
editorLayout->addWidget(editor);
editorLayout->setContentsMargins(0,0,0,0);

this->setLayout(editorLayout);
...


"My add to MDIarea method"


void MainWindow::createEditor()
{
QMdiSubWindow *child = new QMdiSubWindow(componentArea);
TextEditorWidget *editor = new TextEditorWidget(child);

child->setWidget(editor);
child->setAttribute(Qt::WA_DeleteOnClose);

componentArea->addSubWindow(child);
child->show();
}


I have done prety much the same with other widgets, but with this one i get the following problems/errors:

when the method is called the following apears on the console:
"QMdiArea::addSubWindow: window is already added"

and The window itself does not get painted properly at first, only after i move it it gets drawn properly. I have attached a screenshot with a before-and-after.

I just can't figure out why it's giving me problems with this widget, it works fine for a QListwidget subclass and other QFrame sublcasses.

Maybe someone has already encountered this problem and can point me in the right directions.

Thanks for reading.

Eric

caduel
18th September 2009, 19:54
try not passing componentArea as parent when you create that QMdiSubWindow

scascio
19th September 2009, 01:09
Why not inherit directly from QMdiSubWindow instead of adding a new QMdiSubWindow initialized with a QFrame?
QMdiSubWindow does not have all the features of the QFrame?


class TextEditor : public QMdiSubWindow {
Q_OBJECT

public:
TextEditor(QWidget *parent = 0):QMdiSubWindow(parent) {
setAttribute(Qt::WA_DeleteOnClose);
m_text = new QTextEdit;
layout()->addWidget(m_text);
}
~TextEditor() {
}

private:
QWidget * m_text;
};



class MDITextEditor : public QMainWindow {
Q_OBJECT

public:

MDITextEditor(QWidget *parent = 0):QMainWindow(parent) {
m_area = new QMdiArea;
setCentralWidget(m_area);
createActions();
createMenus();
}

~MDITextEditor(){
}

public slots:
void newSubWindow();

private:

QMdiArea * m_area;
QAction * m_new;
QMenu * m_menu;

void createActions() {
m_new = new QAction(this);
m_new->setText(tr("New..."));
connect(m_new, SIGNAL(triggered()), this, SLOT(newSubWindow()));
}
void createMenus() {
m_menu = new QMenu(tr("File"));
m_menu->addAction(m_new);
menuBar()->addMenu(m_menu);
}
};
inline void MDITextEditor::newSubWindow(){
TextEditor * textEditor = new TextEditor;
m_area->addSubWindow(textEditor);
textEditor->show();
}