PDA

View Full Version : open a second window/widget



michaelschmid
10th February 2006, 12:01
Hi to everybody

first, I have seen a similar posting which doesn't solve my problems.
I do my first steps with QT and I tried to combine two examples..

1. -so I compiled the example "application" .. that's working
2. - than I addad a new menu... I can see this.. that's ok..
3. - I treid to open the previewWindow from the example "Windows flags", but this doesn't work..

void MainWindow::indexEnv()
{
previewWindow = new PreviewWindow(this);
previewWindow->activateWindow();
previewWindow->show();
}

indexEnv is the menu and the code insight should open the new widget... but nothing happens..
can someone help?
Thanks in advanced

Cesar
10th February 2006, 13:20
Let me guess... If you modified the application example you should have the following:


//mainwindow.h
//[SKIP]
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
//[SKIP]
private slots:
void newFile();
//[SKIP]
void documentWasModified();
void indexEnv(); // (1)
private:
void createActions();
//[SKIP]
QMenu *editMenu;
QMenu *helpMenu;
QMenu *yourOwnMenu; // (3)
//[SKIP]
QAction *aboutAct;
QAction *aboutQtAct;
QAction *showYourOwnMenuAction; // (2)
};

//mainwindow.cpp
//[SKIP]
void MainWindow::createActions()
{
//[SKIP]
showYourOwnMenuAction = new QAction(tr("Show Preview Window"), this); // (2)
showYourOwnMenuAction->setStatusTip(tr("Show Preview Window from the <i>Windows flags</i> example"));
connect(showYourOwnMenuAction, SIGNAL(triggered()), this, SLOT(indexEnv())); // (5)
//[SKIP]
}
void MainWindow::createMenus()
{
//[SKIP]
yourOwnMenu = menuBar()->addMenu(tr("&Your Menu")); // (3)
yourOwnMenu->addAction(showYourOwnMenuAction); // (4)
//[SKIP]
}

To achieve the result you need you should do these steps:

Implement your indexEnv() as a slot, so that you can connect() to it
Create a QAction, which is to be triggered() when the user clicks on the corresponding menu item
Create the menu itself
Make your action available through the menu
connect() the QAction's triggered() signal to your MainWindow's indexEnv() slot

michaelschmid
10th February 2006, 18:49
sorry.. only a short test.. the answer will follow


//mainwindow.h
//[SKIP]
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

protected:
void closeEvent(QCloseEvent *event);

private slots:
// [SKIP]
void about();
void indexEnv(); // first steps
void documentWasModified();

private:
void createActions();
//[SKIP]

PreviewWindow *previewWindow; // first call of a sub dialog

QTextEdit *textEdit;
QString curFile;

QMenu *fileMenu;
QMenu *editMenu;
QMenu *indexMenu; // first steps to create an index menu --> my own menu
QMenu *helpMenu;


QToolBar *fileToolBar;
QToolBar *editToolBar;
//[SKIP]
QAction *aboutQtAct;
QAction *indexEnvAct; // first steps to create an index menu --> Action for my own menu
};

// mainwindow.cpp
//[SKIP]
void MainWindow::createActions()
{
//[SKIP]
aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

indexEnvAct = new QAction(tr("&Index Environment settings"), this); // my own action and menu and so on ...
indexEnvAct->setStatusTip(tr("Opens the index environment setting dialog"));
connect(indexEnvAct, SIGNAL(triggered()), qApp, SLOT(indexEnv()));

cutAct->setEnabled(false);
copyAct->setEnabled(false);
connect(textEdit, SIGNAL(copyAvailable(bool)),
cutAct, SLOT(setEnabled(bool)));
connect(textEdit, SIGNAL(copyAvailable(bool)),
copyAct, SLOT(setEnabled(bool)));
}

void MainWindow::createMenus()
{
//[SKIP]

menuBar()->addSeparator();

indexMenu = menuBar()->addMenu(tr("&Index"));
indexMenu->addAction(indexEnvAct);
}

void MainWindow::indexEnv() // this here is the last part...
{
previewWindow = new PreviewWindow(this);
previewWindow->activateWindow();
previewWindow->show();
}



so, now I'm hopping, there is somewhere an error... which I can't find...


// previewwindow.h
#ifndef PREVIEWWINDOW_H
#define PREVIEWWINDOW_H

#include <QWidget>

class QPushButton;
class QTextEdit;

class PreviewWindow : public QWidget
{
Q_OBJECT

public:
PreviewWindow(QWidget *parent = 0);

// void setWindowFlags(Qt::WindowFlags flags);

private:
QTextEdit *textEdit;
QPushButton *closeButton;
};

#endif

// previewwindow.cpp

#include <QtGui>

#include "previewwindow.h"

PreviewWindow::PreviewWindow(QWidget *parent)
: QWidget(parent)
{
textEdit = new QTextEdit;
textEdit->setReadOnly(true);
textEdit->setLineWrapMode(QTextEdit::NoWrap);

closeButton = new QPushButton(tr("&Close"));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textEdit);
layout->addWidget(closeButton);
setLayout(layout);

setWindowTitle(tr("Preview"));
}


Is there an error??
For me it seems correct... the 5 steps you described above seems for me to realised..

thanks in advanced