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

      </body></html>