[vtkusers] Simple VTK, QT, and QVTKWidget Problem

John Smith af3113 at yahoo.com
Thu Oct 23 06:11:50 EDT 2008


Hello,

I want to have a simple image viewer. I use vtkDICOMImageReader to read DICOM images and open it in QVTKWidget on Qt Mainwindow. The problem that the QVTKWidget can be attached to the main-window ONLY if I set the QVTWidget as centralWidget. I tried to make vertical layout and I added the QVTKWidget and a slider to it, but the slider doesn't appear at all and the QVTKWidget opens in a separate window (separate from the main-window). Can Anyone help? The code is attached below.

Thanks, J.

Code:

##############################################
main.cpp

#include <QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

################################################
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVBoxLayout>
#include <QSlider>

#include "vtkImageActor.h"
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkTestUtilities.h"
#include "vtkDICOMImageReader.h"

#include "QVTKWidget.h"

class QAction;
class QMenu;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();

protected:
    void closeEvent(QCloseEvent *event);

private slots:
        void about();
        void open();

private:
    void createActions();
    void createMenus();
    void createToolBars();
    void createStatusBar();
    void createSliders();
    void createQVTKWidget();
    void createLayouts();

    void loadFile(const QString &fileName);
    void setCurrentFile(const QString &fileName);
    QString strippedName(const QString &fullFileName);

    QVTKWidget *vtkWidget;
    QString curFile;
    QMenu *fileMenu;
    QMenu *helpMenu;
    QToolBar *fileToolBar;
    QToolBar *helpToolBar;
    QAction *actionOpen;
    QAction *actionExit;
    QAction *actionAbout;
    QAction *aboutQtAct;
    QSlider *sliceSlider;
    QVBoxLayout *mainLayout;

    vtkDICOMImageReader* reader;
    vtkImageViewer* imageViewer;
    int wMainWindow;
    int hMainWindow;
};
#endif

##############################################
mainwindow.cpp

#include <QtGui>

#include "mainwindow.h"

MainWindow::MainWindow()
{    

    createActions();
    createMenus();
    createToolBars();
    createStatusBar();
    createLayouts();
    createQVTKWidget();
    createSliders();
    setCurrentFile("");

    setMinimumSize(800,600);
    resize(800,600);

    wMainWindow = width();
    hMainWindow = height();

    setLayout(mainLayout);
    setWindowIcon(QIcon("C:/icons/qt-logo.png"));

}

void MainWindow::closeEvent(QCloseEvent *event)
{
    imageViewer->Delete();
    reader->Delete();
}

void MainWindow::open()
{
    QString fileName = QFileDialog::getOpenFileName(this);
    if (!fileName.isEmpty())
        loadFile(fileName);
}

void MainWindow::about()
{
    QMessageBox::about(this, tr("About Application"),
        tr("The <b>Application</b> example demonstrates how to "
        "write modern GUI applications using Qt, with a menu bar, "
        "toolbars, and a status bar."));
}

void MainWindow::createActions()
{
    actionOpen = new QAction(QIcon("C:/icons/Folder_64.png"), tr("&Open..."), this);
    actionOpen->setShortcut(tr("Ctrl+O"));
    actionOpen->setStatusTip(tr("Open an existing file"));
    connect(actionOpen, SIGNAL(triggered()), this, SLOT(open()));

    actionExit = new QAction(QIcon("C:/icons/Logoff_64.png"),tr("E&xit"), this);
    actionExit->setShortcut(tr("Ctrl+Q"));
    actionExit->setStatusTip(tr("Exit the application"));
    connect(actionExit, SIGNAL(triggered()), this, SLOT(close()));

    actionAbout = new QAction(QIcon("C:/icons/Info_64.png"),tr("&About"), this);
    actionAbout->setStatusTip(tr("Show the application's About box"));
    connect(actionAbout, SIGNAL(triggered()), this, SLOT(about()));

    aboutQtAct = new QAction(QIcon("C:/icons/qt-logo.png"),tr("About &Qt"), this);
    aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(actionOpen);
    fileMenu->addSeparator();
    fileMenu->addAction(actionExit);

    menuBar()->addSeparator();

    helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(actionAbout);
    helpMenu->addAction(aboutQtAct);
}

void MainWindow::createToolBars()
{
    fileToolBar = addToolBar(tr("File"));
    fileToolBar->addAction(actionOpen);
    fileToolBar->addAction(actionExit);

    helpToolBar = addToolBar(tr("Help"));
    helpToolBar->addAction(actionAbout);
    helpToolBar->addAction(aboutQtAct);

}

void MainWindow::createStatusBar()
{
    statusBar()->showMessage(tr("Ready"));
}

void MainWindow::createQVTKWidget()
{
    vtkWidget = new QVTKWidget;
    mainLayout->addWidget(vtkWidget);
}

void MainWindow::createLayouts()
{
    mainLayout = new QVBoxLayout;
}

void MainWindow::createSliders()
{
    sliceSlider = new QSlider(Qt::Horizontal);
    mainLayout->addWidget(sliceSlider);
}

void MainWindow::loadFile(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly)) {
        QMessageBox::warning(this, tr("Application"),
            tr("Cannot read file %1:\n%2.")
            .arg(fileName)
            .arg(file.errorString()));
        return;
    }


    QApplication::setOverrideCursor(Qt::WaitCursor);

    reader = vtkDICOMImageReader::New();
    reader->SetFileName(fileName.toLatin1());
    imageViewer = vtkImageViewer::New();
    imageViewer->SetInputConnection(reader->GetOutputPort());
    vtkWidget->SetRenderWindow(imageViewer->GetRenderWindow());
    imageViewer->SetupInteractor(vtkWidget->GetRenderWindow()->GetInteractor());
    imageViewer->SetColorLevel(128);
    imageViewer->SetColorWindow(256);
    imageViewer->SetSize(vtkWidget->geometry().width(),vtkWidget->geometry().height());
    vtkWidget->show();

    QApplication::restoreOverrideCursor();

    setCurrentFile(fileName);
    statusBar()->showMessage(tr("File loaded"), 2000);
}


void MainWindow::setCurrentFile(const QString &fileName)
{
    curFile = fileName;
    setWindowModified(false);
    QString shownName;
    shownName = strippedName(curFile);
    setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application")));
}

QString MainWindow::strippedName(const QString &fullFileName)
{
    return QFileInfo(fullFileName).fileName();
}
######################################################################



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20081023/cacef6af/attachment.htm>


More information about the vtkusers mailing list