<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div>Hello,<br><br>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.<br><br>Thanks, J.<br><br>Code:<br><br>##############################################<br>main.cpp<br><br>#include <QApplication><br><br>#include "mainwindow.h"<br><br>int main(int argc, char *argv[])<br>{<br> QApplication app(argc, argv);<br> MainWindow
mainWin;<br> mainWin.show();<br> return app.exec();<br>}<br><br>################################################<br>mainwindow.h<br><br>#ifndef MAINWINDOW_H<br>#define MAINWINDOW_H<br><br>#include <QMainWindow><br>#include <QVBoxLayout><br>#include <QSlider><br><br>#include "vtkImageActor.h"<br>#include "vtkImageViewer.h"<br>#include "vtkRenderWindowInteractor.h"<br>#include "vtkRenderer.h"<br>#include "vtkRenderWindow.h"<br>#include "vtkTestUtilities.h"<br>#include "vtkDICOMImageReader.h"<br><br>#include "QVTKWidget.h"<br><br>class QAction;<br>class QMenu;<br><br>class MainWindow : public QMainWindow<br>{<br> Q_OBJECT<br><br>public:<br> MainWindow();<br><br>protected:<br> void closeEvent(QCloseEvent *event);<br><br>private slots:<br> void about();<br> void
open();<br><br>private:<br> void createActions();<br> void createMenus();<br> void createToolBars();<br> void createStatusBar();<br> void createSliders();<br> void createQVTKWidget();<br> void createLayouts();<br><br> void loadFile(const QString &fileName);<br> void setCurrentFile(const QString &fileName);<br> QString strippedName(const QString &fullFileName);<br><br> QVTKWidget *vtkWidget;<br> QString curFile;<br> QMenu *fileMenu;<br> QMenu *helpMenu;<br> QToolBar *fileToolBar;<br> QToolBar *helpToolBar;<br> QAction *actionOpen;<br> QAction *actionExit;<br> QAction *actionAbout;<br>
QAction *aboutQtAct;<br> QSlider *sliceSlider;<br> QVBoxLayout *mainLayout;<br><br> vtkDICOMImageReader* reader;<br> vtkImageViewer* imageViewer;<br> int wMainWindow;<br> int hMainWindow;<br>};<br>#endif<br><br>##############################################<br>mainwindow.cpp<br><br>#include <QtGui><br><br>#include "mainwindow.h"<br><br>MainWindow::MainWindow()<br>{ <br><br> createActions();<br> createMenus();<br> createToolBars();<br> createStatusBar();<br> createLayouts();<br> createQVTKWidget();<br> createSliders();<br> setCurrentFile("");<br><br> setMinimumSize(800,600);<br> resize(800,600);<br><br> wMainWindow =
width();<br> hMainWindow = height();<br><br> setLayout(mainLayout);<br> setWindowIcon(QIcon("C:/icons/qt-logo.png"));<br><br>}<br><br>void MainWindow::closeEvent(QCloseEvent *event)<br>{<br> imageViewer->Delete();<br> reader->Delete();<br>}<br><br>void MainWindow::open()<br>{<br> QString fileName = QFileDialog::getOpenFileName(this);<br> if (!fileName.isEmpty())<br> loadFile(fileName);<br>}<br><br>void MainWindow::about()<br>{<br> QMessageBox::about(this, tr("About Application"),<br> tr("The <b>Application</b> example demonstrates how to "<br> "write modern GUI applications using Qt, with a menu bar, "<br> "toolbars, and a status
bar."));<br>}<br><br>void MainWindow::createActions()<br>{<br> actionOpen = new QAction(QIcon("C:/icons/Folder_64.png"), tr("&Open..."), this);<br> actionOpen->setShortcut(tr("Ctrl+O"));<br> actionOpen->setStatusTip(tr("Open an existing file"));<br> connect(actionOpen, SIGNAL(triggered()), this, SLOT(open()));<br><br> actionExit = new QAction(QIcon("C:/icons/Logoff_64.png"),tr("E&xit"), this);<br> actionExit->setShortcut(tr("Ctrl+Q"));<br> actionExit->setStatusTip(tr("Exit the application"));<br> connect(actionExit, SIGNAL(triggered()), this, SLOT(close()));<br><br> actionAbout = new QAction(QIcon("C:/icons/Info_64.png"),tr("&About"), this);<br> actionAbout->setStatusTip(tr("Show the application's About box"));<br>
connect(actionAbout, SIGNAL(triggered()), this, SLOT(about()));<br><br> aboutQtAct = new QAction(QIcon("C:/icons/qt-logo.png"),tr("About &Qt"), this);<br> aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));<br> connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));<br><br>}<br><br>void MainWindow::createMenus()<br>{<br> fileMenu = menuBar()->addMenu(tr("&File"));<br> fileMenu->addAction(actionOpen);<br> fileMenu->addSeparator();<br> fileMenu->addAction(actionExit);<br><br> menuBar()->addSeparator();<br><br> helpMenu = menuBar()->addMenu(tr("&Help"));<br> helpMenu->addAction(actionAbout);<br> helpMenu->addAction(aboutQtAct);<br>}<br><br>void MainWindow::createToolBars()<br>{<br>
fileToolBar = addToolBar(tr("File"));<br> fileToolBar->addAction(actionOpen);<br> fileToolBar->addAction(actionExit);<br><br> helpToolBar = addToolBar(tr("Help"));<br> helpToolBar->addAction(actionAbout);<br> helpToolBar->addAction(aboutQtAct);<br><br>}<br><br>void MainWindow::createStatusBar()<br>{<br> statusBar()->showMessage(tr("Ready"));<br>}<br><br>void MainWindow::createQVTKWidget()<br>{<br> vtkWidget = new QVTKWidget;<br> mainLayout->addWidget(vtkWidget);<br>}<br><br>void MainWindow::createLayouts()<br>{<br> mainLayout = new QVBoxLayout;<br>}<br><br>void MainWindow::createSliders()<br>{<br> sliceSlider = new QSlider(Qt::Horizontal);<br> mainLayout->addWidget(sliceSlider);<br>}<br><br>void MainWindow::loadFile(const QString
&fileName)<br>{<br> QFile file(fileName);<br> if (!file.open(QFile::ReadOnly)) {<br> QMessageBox::warning(this, tr("Application"),<br> tr("Cannot read file %1:\n%2.")<br> .arg(fileName)<br> .arg(file.errorString()));<br> return;<br> }<br><br><br> QApplication::setOverrideCursor(Qt::WaitCursor);<br><br> reader = vtkDICOMImageReader::New();<br> reader->SetFileName(fileName.toLatin1());<br> imageViewer = vtkImageViewer::New();<br> imageViewer->SetInputConnection(reader->GetOutputPort());<br>
vtkWidget->SetRenderWindow(imageViewer->GetRenderWindow());<br> imageViewer->SetupInteractor(vtkWidget->GetRenderWindow()->GetInteractor());<br> imageViewer->SetColorLevel(128);<br> imageViewer->SetColorWindow(256);<br> imageViewer->SetSize(vtkWidget->geometry().width(),vtkWidget->geometry().height());<br> vtkWidget->show();<br><br> QApplication::restoreOverrideCursor();<br><br> setCurrentFile(fileName);<br> statusBar()->showMessage(tr("File loaded"), 2000);<br>}<br><br><br>void MainWindow::setCurrentFile(const QString &fileName)<br>{<br> curFile = fileName;<br> setWindowModified(false);<br> QString shownName;<br> shownName = strippedName(curFile);<br> setWindowTitle(tr("%1[*] -
%2").arg(shownName).arg(tr("Application")));<br>}<br><br>QString MainWindow::strippedName(const QString &fullFileName)<br>{<br> return QFileInfo(fullFileName).fileName();<br>}<br>######################################################################<br></div></div><br>
</body></html>