文档库 最新最全的文档下载
当前位置:文档库 › VTK和QT联合使用

VTK和QT联合使用

VTK和QT联合使用
VTK和QT联合使用

qt和vtk共用

作者:我是小李,欢迎交流来源:博客园发布时间:2009-02-10 11:00 阅读:908 次原文链接[收

藏]

QT与VTK的结合开发

2008/07/16 15:26

最近,由于项目的需要,我打算使用VTK来开发程序,VTK全名是VisualizationToolKit,它是一套跨平台的C++库,对OpenGL作了较全面的封装,将各种三维模型的存储,运算,显示,交互等内容全都以类的方式封装起来了,并且提供比OpenGL强大得多的功能。可惜VTK最大的缺点是,没有免费的教程,它只提供免费的API手册,其中只是对各个类的功能罗列而已,而参考书籍则需要花几十美元去购买,所以学习VTK只能依靠它的大量Example了。

由于我的项目需要兼故未来跨平台的可能(目前只在Windows下),所以必须使用一套跨平台的开发库,在VTK\Examples\GUI的例子里,Windows平台下只有SDK,MFC,C++ Builder这几个例子,另外还有Motif,Python,Tcl,选择是不少,但是Motif听说编程比较麻烦,我也从来没有接触过,Tcl和Python作为脚本语言,其性能和安全性则有些令人担忧,也就是说,这里面没有一个是我能使用的。。。说起跨平台,由于单位里项目的关系,我接触得比较多的就是QT了,所以,在未选定VTK之前,其实我是打算使用QT+OpenGL的组合方式来开发这个软件的,其实,如果不考虑跨平台,我还是会选择QT,因为它的事件处理方式对于用惯Delphi的我而言,更为顺手,那么现在使用了VTK,是否还能将VTK和QT组合起来使用呢。。

作为试验,我仿照VTK\Examples\GUI\Win32\SampleMFC,制作了以下这个小程序,很顺利,结果证明了我的猜想,QT和VTK是可以很方便的结合起来的,下面就跟我来一步步制作这个程序吧:

1)从https://www.wendangku.net/doc/811199966.html,/下载VTK的最新版本,我用的是4.2。该包中只有程序文件,你还需要自己编译,为此你还需要CMake,用来生成VC的.dsw,.dsp文件。然后用VC 打开生成的工程文件,并进行编译。最后将编译后生成的目录:VTK\bin\Debug加入到Windows系统环境的Path中。

2)下载QT,我目前使用的是QT334,打开VC的Tools->Options->Directories,将QT的bin,include,lib路径分别加入其中。

3)在VC中,建立一个新的Win32 Console Application工程,建立一个main.cpp,作为main函数的入口,代码如下:

#include

#include "testwindow.h"

int main(int argc, char** argv)

{

QApplication app(argc, argv);

TestWindow testwin;

testwin.show();

app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));

return app.exec();

}

其中的testwindow.h,是程序的主窗口TestWindow的头文件,一会将会建立该文件。这段程序所做的是,初始化QApplication,并将TestWindow显示出来,并进入主程序的消息循环app.exec()。

下面实现TestWindow类,分别建立testwindow.h和testwindow.cpp testwindow.h:

#include

class TestWindow: public QMainWindow

{

Q_OBJECT

public:

TestWindow();

~TestWindow();

};

testwindow.cpp:

#include "testwindow.h"

#include "moc_testwindow.h"

TestWindow::TestWindow()

{

}

TestWindow::~TestWindow()

{

}

你是否注意到了testwindow.cpp中的#include "moc_testwindow.h"一行,这个moc_testwindow.h将会建立TestWindow的RTTI信息,并且绑定消息等,它并不需要我们自己实现,而是由qt的一个名为moc的程序建立,在VC的FileView中,右键点击testwindow.h,并选择Settings,然后在打开的Project Settings对话框中选择Custom Build页,在Commands中填入:

moc -i testwindow.h -o moc_testwindow.h

意思是调用moc程序,根据testwindow.h的内容,自动生成一个名为

moc_testwindow.h的moc文件。

在Outputs中填入:

moc_testwindow.h

然后,在Project Settings中选中projct,在Link页的Object/library modules中加入:qt-mt334.lib。

编译程序,如果顺利,一个简单的QT程序就写好了,它会在启动后显示一个空白的窗口。3)加入VTK的代码,这些代码都可以参考Examples\GUI\Win32\SampleMFC程序,将testwindow.h改造成如下:

#include

#include "vtkRenderer.h"

#include "vtkWin32OpenGLRenderWindow.h"

#include "vtkWin32RenderWindowInteractor.h"

class TestWindow: public QMainWindow

{

Q_OBJECT

public:

TestWindow();

~TestWindow();

protected:

virtual void paintEvent(QPaintEvent *);

virtual bool winEvent(MSG *);

private:

vtkRenderer *Renderer;

vtkWin32OpenGLRenderWindow *RenderWindow;

vtkWin32RenderWindowInteractor *Interactor;

};

testwindow.cpp改造成如下:

#include "testwindow.h"

#include "moc_testwindow.h"

#include "vtkActor2D.h"

#include "vtkTextMapper.h"

#include "vtkTextProperty.h"

#include "vtkDataSetReader.h"

#include "vtkDataSetMapper.h"

#include "vtkCommand.h"

#include "vtkCamera.h"

#include "vtkWin32RenderWindowInteractor.h"

#include "vtkInteractorStyleTrackballCamera.h" TestWindow::TestWindow()

{

this->Renderer = vtkRenderer::New();

this->Renderer->SetBackground(0.3, 0.5, 0.1);

this->RenderWindow = vtkWin32OpenGLRenderWindow::New(); this->RenderWindow->AddRenderer(this->Renderer);

this->Interactor = vtkWin32RenderWindowInteractor::New(); vtkActor2D *actor2d = vtkActor2D::New();

vtkTextMapper *txt = vtkTextMapper::New();

actor2d->SetMapper(txt);

txt->SetInput("Hello World");

txt->GetTextProperty()->SetFontSize(24);

this->Renderer->AddProp(actor2d);

txt->Delete();

actor2d->Delete();

vtkActor *actor = vtkActor::New();

vtkDataSetReader *reader = vtkDataSetReader::New();

reader->SetFileName("weldedSpheres.vtk"); vtkDataSetMapper *mapper = vtkDataSetMapper::New(); mapper->SetInput(reader->GetOutput());

actor->SetMapper(mapper);

this->Renderer->AddProp(actor);

mapper->Delete();

reader->Delete();

actor->Delete();

}

TestWindow::~TestWindow()

{

if (this->Interactor) {

this->Interactor->Delete();

}

if (this->Renderer) {

this->Renderer->SetRenderWindow(NULL);

}

if (this->RenderWindow) {

this->RenderWindow->Delete();

}

if (this->Renderer) {

this->Renderer->Delete();

}

}

void TestWindow::paintEvent(QPaintEvent *e)

{

if (! this->Interactor->GetInitialized()) {

this->RenderWindow->SetWindowId(this->winId());

this->RenderWindow->WindowInitialize();

this->Interactor->SetRenderWindow(this->RenderWindow);

this->Interactor->Initialize();

}

this->RenderWindow->Render();

}

bool TestWindow::winEvent(MSG *msg)

{

switch (msg->message) {

case WM_LBUTTONDOWN:

case WM_LBUTTONUP:

case WM_MBUTTONDOWN:

case WM_MBUTTONUP:

case WM_RBUTTONDOWN:

case WM_RBUTTONUP:

case WM_MOUSEMOVE:

case WM_CHAR:

case WM_TIMER:

if (this->Interactor->GetInitialized()) {

vtkHandleMessage2(msg->hwnd, msg->message, msg->lParam,

msg->wParam, this->Interactor);

}

}

return false;

}

其中用到了一个模型文件weldedSpheres.vtk,可以在VTK中找到。可能你需要修改它的路径。

然后,再次打开Proect Settings对话框,在C/C++页中,选择Category:Preprocessor,在Additional include directories:中加入:

D:\VTK,D:\VTK\Parallel,D:\VTK\Hybrid,D:\VTK\Patented,D:\VTK\Rendering,D :\VTK\IO,D:\VTK\Imaging,D:\VTK\Graphics,D:\VTK\Filtering,D:\VTK\Commo n

选择Link页,选择Category:Input,在Object/library modules:中加入:vtkRendering.lib vtkGraphics.lib vtkImaging.lib vtkIO.lib vtkFiltering.lib vtkCommon.lib vtkftgl.lib glu32.lib opengl32.lib glu32.lib opengl32.lib vtkfreetype.lib vtkpng.lib vtktiff.lib vtkzlib.lib vtkjpeg.lib vtkexpat.lib

在Addtional library path中加入:

D:\VTK\bin\debug

以上都假设VTK安装在D盘下,如果你安装在其它目录,请修改以上的路径。好了,重新编译程序,运行,你将看到如下所示的VTK界面。

Tuesday, October 17th 2006, 6:13pm

Tutorial for using Qt with VTK

Hi,

I am a newbie in VTK and Qt. For some time now,I have been trying to use VTK with Qt but faced a lot of problems getting started and configuring them. In the end, it worked(thanks to Anja Ende,Clinton Stimpson and everyone who helped me get started). I am writing down the procedure to get VTK to work with Qt in visual studio 2005 environment. Hopefully this document will help other newbies save a lot of time.

I have tested the following procedure with MS Visual Studio 2005 Professional on Windows XP Pro X64.

1). Install and get Qt working as per the instructions here: https://www.wendangku.net/doc/811199966.html,/wiki/Qt4_with_Visual_Studio

This works fine and you can start using Qt with visual studio(for those who just want to use Qt with Visual Studio, this is how we do it!)

2). Install and configure VTK using Cmake with (a) VTK_USE_GUISUPPORT option and (b) VTK_USE_QVTK option, set to 'ON'

If Qt is installed after VTK, you will need to reconfigure VTK to find Qt.

3).Now it's time to test the configuration with a code which uses both Qt and VTK. You can try it with the example in VTK source( ../Examples/GUI/Qt/Imageviewer). Copy this code(only the cxx file) to a new directory.

4) I use 'qmake' to generate build files. The method to build this example using qmake is:

(a) open visual studio command prompt, go to the directory containing the cxx file and type: qmake -project

this makes a '.pro' file. Open this file in an editor and modify it as below-

----

######################################################################

# Automatically generated by qmake (2.00a) Wed Oct 11 17:14:01 2006

######################################################################

TEMPLATE = vcapp # this was originally 'app' modify it to 'vcapp'

TARGET +=

DEPENDPATH += .

INCLUDEPATH += . # here include the path to all the directories containing the header files LIBS += # add this line and include all the libraries(Qt and VTK) needed for the application

# Input

SOURCES += main.cxx

-----

(b)Save the .pro file and run qmake from the command prompt again. This will generate a '.vcproj' file. Open this file in visual studio and build it there. If you included all the libraries and include paths, this should build and run perfectly.

I used qmake for building, because I didn't know how to do it with Cmake. But if the experts here can throw light on that it would help many. Also if someone knows of any other way(probably easier and smarter) to use Qt with VTK, please add to this document.

Thanks,

Ashish

?Go to the top of the page

?Quote

Skip user information

alexiuk

Beginner

Posts: 2

?

?

2

Thursday, May 10th 2007, 6:04pm

RE: Tutorial for using Qt with VTK

Hi Ashish,

Thanks for posting those details. I am trying to follow those instructions and am running into problems building QVTK. Error messages follow. Did you have any trouble building VTK? Thanks,

Mark

1>------ Build started: Project: QVTK, Configuration: Debug Win32 ------

1>Generating moc_vtkEventQtSlotConnect.cxx

1>Generating moc_QVTKWidget.cxx

1>Compiling...

1>moc_vtkEventQtSlotConnect.cxx

1>c1xx : fatal error C1083: Cannot open source file: '.\moc_vtkEventQtSlotConnect.cxx': No such file or directory

1>moc_QVTKWidget.cxx

1>c1xx : fatal error C1083: Cannot open source file: '.\moc_QVTKWidget.cxx': No such file or directory

1>Generating Code...

1>Build log was saved at "file://c:\VTK\vcc_build\GUISupport\Qt\QVTK.dir\Debug\BuildLog.htm"

1>QVTK - 2 error(s), 0 warning(s)

VTK Build summary:

========== Build: 1 succeeded, 3 failed, 47 up-to-date, 6 skipped

?Go to the top of the page

?Quote

Skip user information

Ashish

Beginner

?

Posts: 8

?

?

3

Thursday, May 10th 2007, 8:30pm

RE: Tutorial for using Qt with VTK

Hi Mark,

I didn't face any problems building QVTK. Also I have stopped using 'qmake' and use 'cmake' these days.Can you give me more details as to what you have done so far and at what step are you getting the errors? Are you able to test run a simple Qt code?

Thanks,

Ashish

?Go to the top of the page

?Quote

Skip user information

frenchmikey

Beginner

Posts: 1

?

?

4

Monday, February 16th 2009, 8:41am

VTK with QTcreator

Hi Ashish,

I installed VTK and successfully built all the examples ( specially the GUI/Qt)... everything is working fine when compiled with CMAKE

now l try to compile the same examples with Qtcreator and his qmake funtion... doesn't work

I updated my .pro with the includepath and library ... but doesn't work.

do you have a simple .pro so l can check if l don't miss anything ??

also l'm using mingwin ....

thanks

Michael

my .pro:

CONFIG+= DEBUG TEMPLATE = app

TARGET = VTK_test

DEPENDPATH += .

INCLUDEPATH +=C:\VTK\bin \

C:\VTK\include\vtk-5.2

LIBS +=

# Input

SOURCES += main.cxx

Compile messages:

Starting: C:/Qt/QtCreator/mingw/bin/mingw32-make.exe debug -w

mingw32-make: Entering directory `C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtk'

C:/Qt/QtCreator/mingw/bin/mingw32-make -f Makefile.Debug

mingw32-make[1]: Entering directory `C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtk'

g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"c:\Qt\QtCreator\qt\include\QtCore" -I"c:\Qt\QtCreator\qt\include\QtCore" -I"c:\Qt\QtCreator\qt\include\QtGui" -I"c:\Qt\QtCreator\qt\include\QtGui" -I"c:\Qt\QtCreator\qt\include" -I"c:\VTK\bin" -I"c:\VTK\include\vtk-5.2" -I"c:\Qt\QtCreator\qt\include\ActiveQt" -I"debug" -I"." -I"c:\Qt\QtCreator\qt\mkspecs\win32-g++" -o debug\main.o main.cxx

mingw32-make[1]: Leaving directory `C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtk'

mingw32-make: Leaving directory `C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtk'

This application has requested the Runtime to terminate it in an unusual way.

Please contact the application's support team for more information.

mingw32-make[1]: *** [debug/main.o] Error 3

mingw32-make: *** [debug] Error 2

Exited with code 2.

Error while building project vtk

When executing build step 'Make'

?Go to the top of the page

?Quote

Skip user information

xzh_biti

Beginner

Posts: 1

?

?

5

Wednesday, March 11th 2009, 1:46am

hello ,everyone

hello, everyone, i am a foreigner, and i am poor in English, so forgive me my English.

So i want to ask Michael , have your solved your problem with qtcreator to compile VTK, if you have, could you tell me how to write .pro , and do you have some vtk electrical books ?In my country, so few information about vtk, and please send me some of them , thank you very much!

And my email is : xzh_biti@https://www.wendangku.net/doc/811199966.html,

?Go to the top of the page

?Quote

Skip user information

justadreamer

Beginner

Posts: 2

?

?

6

Friday, April 24th 2009, 10:13pm

QtCreator + QT + VTK in Linux

Hi, just wanted to share my experience on a jump start with using Qt+VTK.

As I don't have experience with using Qt I decided to go the easiest possible way - downloaded Qt4 and the QtCreator.

The steps I took next:

1. Built Qt4 from source and installed it.

2. Added /usr/local/Trolltech to my PA TH environment variable

3. Downloaded the source for VTK 5.4

4. Launched ccmake in the root of the source tree.

5. Switched ON VTK_USE_GUISUPPORT VTK_USE_QVTK.

6. Pressed "c" to configure in ccmake, and specified the version of Qt 4.0.

7. After configuring in ccmake, launched cmake, and then make and make install.

8. The needed QVTKWidget.h will be in the /usr/bin/local/include/vtk-5.4.

9. Now we launch QtCreator and create the simple application.

10. In the mainwindow.h I add these headers:

Source code

1 2 3 #include

#include

#include

And these members:

Source code 1 2 QVTKWidget* vtkWidget;

vtkRenderer* ren;

11. In the mainwindow.cpp I added this code in the constructor:

Source code 1

2

3

4 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowClass) { ui->setupUi(this); vtkWidget = new QVTKWidget(this,QFlag(0));

5 6 7 8 9 10 ui->verticalLayout->addWidget(vtkWidget);

ui->verticalLayout->update();

ren = vtkRenderer::New();

vtkWidget->GetRenderWindow()->AddRenderer(ren);

ren->SetBackground(1.0,0,0);

ren->Render(); }

And destructor:

Source code 1

2

3

4 MainWindow::~MainWindow() { ren->Delete(); delete vtkWidget; delete ui; }

As you see above I have added a verticalLayout to my form - it has some more controls, and the widget I am adding goes to the bottom.

12. In the .pro file I added these:

Source code 1 2 3 LIBS += -L/usr/local/lib/vtk-5.4 -lvtkCommon -lvtksys -lQVTK -lvtkQtChart -lvtkViews -lvtkWidgets -lvtkInfovis -lvtkRendering -lvtkGraphics -lvtkImaging -lvtkIO -lvtkFiltering -lvtklibxml2 -lvtkDICOMParser -lvtkpng -lvtkpng -lvtktiff -lvtkzlib -lvtkjpeg -lvtkalglib -lvtkexpat -lvtkverdict -lvtkmetaio -lvtkNetCDF -lvtksqlite -lvtkexoIIc -lvtkftgl -lvtkfreetype -lvtkHybrid

INCLUDEPATH += /usr/local/include/vtk-5.4

13. In the Project->Build settings->Build environment I added a variable LD_LIBRARY_PATH = /usr/local/lib/vtk-5.4 - so that when you launch the application the dynamic linker finds the libraries.

14. After building and running (Press [F5] in Qt creator) it should show you a red QVTKWidget.

?

Go to the top of the page ?

Quote

John431

Beginner

Posts: 1

?

?

7

Saturday, July 4th 2009, 3:58am

tried on window xp, cannot make it work

I followed information here trying to make it work on QT4.5 and vtk5.4 to work on MinGW with qtcreater1.2. I managed to figure out most of the steps based on information on this tutorial. I cannot make it linked right. It always report undefined reference of vtk's class. Can anyone with experience to show me what is wrong here.

I compiled vtk with QT support under MinGW successfully and installed vtk library. It is in static library form. I add both include and library into .pro file.

I am new to qt. Please help here if you know or have similar experience.

Error from the compiler

undefined reference to `vtkActor::New()'

?Go to the top of the page

?Quote

justadreamer

Beginner

Posts: 2

?

?

8

Wednesday, January 13th 2010, 2:36pm

The example I posted uses gcc under linux, so the LIBS and INCLUDEPA TH variables as well as the paths (/usr/...) might not be relevant for MinGW compiler. undefined reference vtkActor::New() is for sure the linker error - meaning that the library containing this function is not linked with. check your paths and/or settings needed to be passed to MinGW. I have never used MinGW therefore I can not help you more precisely.

布局管理器主要包括

布局管理器主要包括:FlowLayout,BorderLayout,GridLayout,CardLayout,GridBagLayout 例3 import java.awt.*; public class ExGui{ private Frame f; private Button b1; private Button b2; public static void main(String args[]){ ExGui that = new ExGui(); that.go(); } public void go(){ f = new Frame("GUI example"); f.setLayout(new FlowLayout()); //设置布局管理器为FlowLayout b1 = new Button("Press Me"); //按钮上显示字符"Press Me" b2 = new Button("Don't Press Me"); f.add(b1); f.add(b2); f.pack(); //紧凑排列,其作用相当于setSize(),即让窗口 尽量小,小到刚刚能够包容住b1、b2两个按钮 f.setVisible(true); } } 查看运行结果 1. FlowLayout FlowLayout 是Panel,Applet的缺省布局管理器。其组件的放置规律是从上到下、从左到右进行放置,如果容器足够宽,第一个组件先添加到容器中第一行的最左边,后续的组件依次添加到上一个组件的右边,如果当前行已放置不下该组件,则放置到下一行的最左边。 构造方法主要下面几种: FlowLayout(FlowLayout.RIGHT,20,40); /*第一个参数表示组件的对齐方式,指组件在这一行中的位置是居中对齐、居右对齐还是居左对齐,第二个参数是组件之间的横向间隔,第三个参数是组件之间的纵向间隔,单位是象素。*/ FlowLayout(FlowLayout.LEFT);

GroupLayout布局管理器介绍

GroupLayout布局管理器介绍 GroupLayout 是一个 LayoutManager,它将组件按层次分组,以决定它们在Container 中的位置。GroupLayout 主要供生成器使用,但也可以手工编码。分组由Group类的实例来完成。GroupLayout 支持两种组。串行组 (sequential group) 按顺序一个接一个地放置其子元素。并行组 (parallel group) 能够以四种方式对齐其子元素。 每个组可以包含任意数量的元素,其中元素有 Group、Component 或间隙 (gap)。间隙可被视为一个具有最小大小、首选大小和最大大小的不可见组件。此外,GroupLayout 还支持其值取自 LayoutStyle 的首选间隙。 元素类似于一个弹簧。每个元素都有一个范围,由最小大小、首选大小和最大大小指定。间隙的范围由开发人员指定,或者由 LayoutStyle 确定。Component 的范围通过 Component 的 getMinimumSize、getPreferredSize 和getMaximumSize 方法确定。此外,添加 Component 时,可以指定使用一个特定范围,而不使用该组件的范围。Group 的范围由组的类型确定。ParallelGroup 的范围是其元素范围的最大值。SequentialGroup 的范围是其元素范围的总和。 GroupLayout 将每个轴 (axis) 单独对待。也就是说,存在一个表示水平轴的组和一个表示垂直轴的组。水平组负责确定沿水平轴的最小大小、首选大小和最大大小,并设置所包含组件的 x 和宽度。垂直组负责确定沿垂直轴的最小大小、首选大小和最大大小,并设置所包含组件的 y 和高度。每个 Component 都必须同时存在于水平组和垂直组中,否则,在布局过程中或者在请求最小大小、首选大小或最大大小时,将抛出 IllegalStateException。 下图显示了一个沿水平轴的串行组。该串行组包含三个组件。沿垂直轴使用了一个并行组。 为了强调要单独对待每个轴,该图显示了沿每个轴的每个组和元素的范围。每个组件的范围已被投射到轴上,两个组分别呈现为蓝色(水平)和红色(垂直)。为了便于理解,串行组中的每个元素之间都有一个间隙。 沿水平轴的串行组呈现为蓝色实线。注意,串行组是它所包含的子元素的总和。 沿垂直轴,并行组是每个组件高度的最大值。由于三个组件的高度都相同,所以并行组具有相同的高度。

GridBagLayout(网格布局管理器)结构分析与功能使用详解

java.awt 类 GridBagLayout https://www.wendangku.net/doc/811199966.html,ng.Object java.awt.GridBagLayout 所有已实现的接口: LayoutManager, LayoutManager2, Serializable public class GridBagLayout extends Object implements LayoutManager2, Serializable GridBagLayout类是一个灵活的布局管理器,它不要求组件的大小相同便可以将组件垂直、水平或沿它们的基线对齐。每个GridBagLayout对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,该单元被称为显示区域。 每个由GridBagLayout管理的组件都与GridBagConstraints的实例相关联。Constraints 对象指定组件的显示区域在网格中的具体放置位置,以及组件在其显示区域中的放置方式。除了 Constraints 对象之 外,GridBagLayout还考虑每个组件的最小大小和首选大小,以确定组件的大小。 网格的总体方向取决于容器的ComponentOrientation属性。对于水平的从左到右的方向,网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增。对于水平的从右到左的方向,网格坐标 (0,0) 位于容器的右上角,其中 X 向左递增,Y 向下递增。 为了有效使用网格包布局,必须自定义与组件关联的一个或多个GridBagConstraints对象。可以通过设置一个或多个实例变量来自定义GridBagConstraints对象: GridBagConstraints.gridx、GridBagConstraints.gridy 指定包含组件显示区域的前导角的单元,在此显示区域中,位于 网格原点的单元地址是 gridx = 0, gridy = 0。对于水平的从左到右的布局,组件的前导角是其左上角。对于水平的从右到左的 布局,组件的前导角是其右上角。使用 GridBagConstraints.RELATIVE(默认值),指定会将组件直接放置在之前刚添加到容器中的组件的后面(沿 X 轴向为 gridx 或Y 轴向为 gridy)。 GridBagConstraints.gridwidth、GridBagConstraints.gridheight

android实验3界面设计:布局管理器

西安邮电大学 (计算机学院) 课内实验报告实验名称:界面设计:布局管理器 专业:网络工程 班级: 姓名: 学号: 指导教师: 日期:2017年5月4日

一.实验目的 1. 了解四种布局管理器的区别和各自特别的属性 2. 掌握四种布局管理器的应用场合和用法 3. 灵活使用四种布局文件管理器和嵌套实现各种复杂布局 4. 掌握复用XML布局文件的方法 5. 掌握代码控制UI界面的方法 二.实验环境 JDK的版本: "1.8.0_40" IDE: eclipse 4.6.1 模拟器: 夜神模拟器 三.实验内容 设计的样式,完成Android应用UI的应用开发 四.实验过程及分析 1.用JA V A代码设置全屏 打开工程src目录下的主Activity文件,在onCreate方法中的执行语句super. onCreate(savedInstanceState)之前,添加如下两句语句 requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏标题栏this.getWindow().setFlags(https://www.wendangku.net/doc/811199966.html,youtParams.FLAG_F ULLSCREEN,https://www.wendangku.net/doc/811199966.html,youtParams.FLAG_FULLSCREEN); //隐藏运营商图标、电量等 2. 按照图1设计的样式,完成一个Android应用UI的开发 (1)添加代码

JAVA布局管理器

前言 随着Internet的飞速发展,Java技术也得到了越来越广泛的应用。而无论我们是采用J2SE、J2EE还是J2ME,GUI都是不能回避的问题。现在的应用软件越来越要求界面友好、功能强大而又使用简单。而众所周知,在Java中进行GUI设计相对于其跨平台、多线程等特性的实现要复杂和麻烦许多。这也是很多Java 程序员抱怨的事情。但GUI已经成为程序发展的方向,所以我们也必须了解Java 的GUI设计方法和特点。其实,采用Java提供的布局管理器接口和相应的布局管理类,我们也可以做出相当漂亮的界面来,当然实现起来肯定要比VB麻烦许多。本文试图通过自己的开发经历介绍一些具体的应用实例,希望能给那些曾经象我一样苦闷的Java痴迷者一些帮助。 2 Java中的布局管理器 2.1 为什么要使用布局 在实际编程中,我们每设计一个窗体,都要往其中添加若干组件。为了管理好这些组件的布局,我们就需要使用布局管理器。比如说,设计一个简单的计算器,或一个文本编辑器等等。这些组件是让JVM 自己任意安排呢?还是按照一定的位置关系进行规范的安排呢?当然应该选择后者。将加入到容器的组件按照一定的顺序和规则放置,使之看起来更美观,这就是布局。在Java中,布局由布局管理器 (LayoutManager) 来管理。那么,我们在什么时候应该使用布局管理器?应选择哪种布局管理器?又该怎样使用布局管理器呢? 如果你写的是GUI程序,在使用AWT/Swing组件时就不应硬性设置组件的大小和位置,而应该使用Java的布局管理器(LayoutManager)来设置和管理可视组件的大小和位置,否则就有可能造成布局混乱。不信,你可以新建一个Frame(或JFrame),通过setBounds()方法往其中添加几个Button(或JButton),一旦你将窗体拉大或缩小时,你会发现组件的排列完全不是按你所预想的那样。为了解决这个问题,即当窗体(或容器)缩放时,组件位置也随之合理调整,我们就需要使用布局管理器。 为此,我们首先要知道Java的布局方式,Java提供的API中有些什么布局管理器,它们的布局特点是什么。 2.2 Java的布局方式 我们都知道,Java的GUI界面定义是由AWT类包和Swing类包来完成的。它在布局管理上采用了容器和布局管理分离的方案。也就是说,容器只管将其他组件放入其中,而不管这些组件是如何放置的。对于布局的管理交给专门的布局管理器类(LayoutManager)来完成。 现在我们来看Java中布局管理器的具体实现。我们前面说过,Java中的容器类(Container),它们只管加入组件(Component),也就是说,它只使用自己的

实验2 UI界面开发--布局管理器的使用

实验2、View及布局管理器的使用 一、实验目的 1、了解View类、ViewGroup的作用 2、掌握常用布局管理器的使用 3、掌握XML界面控制的设计方法 4、从本周起,以后每次小课的实验内容,要求上传FTP。 二、实验步骤 1、线性布局的例子,补充代码,使程序运行效果如下所示 图1 线性布局 LinearlayoutDemo.java代码不用做任何修改,直接通过main.xml文件控制UI 界面。 下面是布局文件main.xml中的内容,请补充完整,实现图1中的界面: //排列方式(horizontal水平排列,vertical垂直排列)

android:layout_height="wrap_content" android: layout_weight ="2"//设置所占相对宽度 android:background="#aa0000" android:gravity="center_vertical"//设置主件对齐方式 android:singleLine="true"//若为true,看不到的文字用……表示 android:text=" 广州大学华软软件学院" android:textSize="15pt"/> 2、相对布局管理器的使用,补充代码,使程序运行的效果如下图所示: 图2相对布局运行效果 RelativeLayoutDemo.java中的源代码如下所示:

布局管理器使用

布局管理器的使用 在前一篇中我们学习了使用资源文件为主窗口添加菜单图标。这次,我们先将菜单进行完善,然后讲解一些布局管理方面的内容。 一、完善菜单 1.新建Qt Gui应用,项目名称为myMainWindow,基类选择QMainWindow,类名为MainWindow。 2.完成后,在设计模式添加菜单项,并添加资源文件,向其中添加菜单图标。最终各个菜单如下图所示。 在Action编辑器中修改动作的对象名称、图标和快捷键,最终如下图所示。 二、向工具栏添加菜单图标

可以将动作编辑器中的动作拖动到工具栏中作为快捷图标使用,如下图所示。 可以在工具栏上点击鼠标右键来添加分隔符,如下图所示。 最终工具栏如下图所示。 三、布局管理器 1.从左边控件栏中拖入三个按钮Push Button和一个Vertical Layout(垂直布局管理器)到界面上,如下图所示。

然后将三个按钮拖入到布局管理器中,这时三个按钮就会自动垂直排列,并且进行水平拉伸,无论如何改变布局管理器的大小,按钮总是水平方向变化。如下图所示。 2.我们可以选中布局管理器,然后按下上方工具栏中的“打破布局”按钮来删除布局管理器。(当然也可以先将三个按钮移出,然后按下Delete键来删除布局管理器,如果不移出按钮,那么会将它们同时删除。)如下图所示。

3.下面我们使用分裂器(QSplitter)来进行布局,先同时选中三个按钮,然后按下上方工具栏中的“使用分裂器垂直布局”按钮,如下图所示。 然后我们进行放大,可以发现,使用分裂器按钮纵向是可以变大的,这就是分裂器和布局管理器的重要区别。如下图所示。 4.布局管理器除了可以对部件进行布局以外,还有个重要用途,就是使部件随着窗口的大小变化而变化。我们删除界面上的部件,然后拖入一个文本编辑器Text Edit部件。如下图所示。

相关文档
相关文档 最新文档