Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 74 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
74
Dung lượng
218,07 KB
Nội dung
575 Chapter 19 ✦ The Widgets of KDE QPopupMenu *popup, bool enabled, const QString &_text, int index = - 1); int insertCombo(QStrList *list, int id, bool writable, const char *signal, const QObject *receiver, const char *slot, bool enabled = true, const QString &tooltiptext = QString::null, int size = 70, int index = - 1, QComboBox::Policy policy = QComboBox::AtBottom); int insertCombo(const QStringList &list, int id, bool writable, const char *signal, const QObject *receiver, const char *slot, bool enabled = true, const QString &tooltiptext = QString::null, int size = 70, int index = - 1, QComboBox::Policy policy = QComboBox::AtBottom); int insertCombo(const QString &text, int id, bool writable, const char *signal, QObject *recevier, const char *slot, bool enabled = true, const QString &tooltiptext = QString::null, int size = 70, int index = - 1, QComboBox::Policy policy = QComboBox::AtBottom); void insertComboItem(int id, const QString &text, int index); void insertComboList(int id, QStrList *list, int index); void insertComboList(int id, const QStringList &list, int index); int insertLineSeparator(int index = - 1); int insertLined(const QString &text, int ID, const char *signal, const QObject *receiver, const char *slot, bool enabled = true, const QString &toolTipText = QString::null, int size = 70, int index = - 1); int insertSeparator(int index = - 1); int insertWidget(int id, int width, QWidget *_widget, int index = - 1); bool isButtonOn(int id); int maxHeight(); int maxWidth(); virtual QSize maximumSizeHint() const; virtual QSize minimumSizeHint() const; void removeComboItem(int id, int index); void removeItem(int id); void saveState(); void setAutoRepeat(int id, bool flag = true); void setBarPos(BarPosition bpos); void setButton(int id, bool flag); void setButtonIcon(int id, const QString &_icon); void setButtonPixmap(int id, const QPixmap &_pixmap); void setCurrentComboItem(int id, int index); void setDelayedPopup(int id, QPopupMenu *_popup, bool toggle = false); void setEnableContextMenu(bool enable = true); void setFlat(bool flag); 4682-1 ch19.f.qc 11/20/00 15:44 Page 575 576 Part III ✦ Reference and Mechanics void setFullSize(bool flag = true); void setIconSize(int size); void setIconSize(int size, bool update); void setIconText(IconText it); void setIconText(IconText it, bool update); void setItemAutoSized(int id, bool yes = true); void setItemEnabled(int id, bool enabled); void setItemNoStyle(int id, bool no_style = true); void setLinedText(int id, const QString &text); void setMaxHeight(int h); void setMaxWidth(int dw); void setTitle(const QString &_title); void setToggle(int id, bool flag = true); void setXML(const QString &xmlfile, const QDomDocument &xml); void showItem(int id); virtual QSize sizeHint() const; virtual QSizePolicy sizePolicy() const; void toggleButton(int id); void updateRects(bool resize = false); virtual int widthForHeight(int height) const; Signals void clicked(int id); void doubleClicked(int id); void highlighted(int id, bool isHighlighted); void highlighted(int id); void modechange(); void moved(BarPosition); void pressed(int); void released(int); void toggled(int); Enums enum IconText { IconOnly=0, IconTextRight, TextOnly, IconTextBottom }; enum BarStatus { Toggle, Show, Hide }; enum BarPosition { Top=0, Left, Right, Bottom, Floating, Flat }; There are several examples of the KToolBar widget in Chapter 6. KToolBarButton The KToolBarButton widget is used internally by the KToolBar widget to display buttons that respond to the mouse. Cross- Reference 4682-1 ch19.f.qc 11/20/00 15:44 Page 576 577 Chapter 19 ✦ The Widgets of KDE The KToolBarButton is constructed by calling one of the insertButton() meth- ods of KToolBar. Using the ID number, a call to the KToolBar method getButton() can be used to retrieve a pointer to the button, making the methods listed here available. File #include <ktoolbarbutton.h> Base Classes QButton QObject QPaintDevice QWidget Qt Constructors KToolBarButton(const QString &icon, int id, QWidget *parent, const char *name = 0L, const QString &txt = QString::null); KToolBarButton(const QPixmap &pixmap, int id, QWidget *parent, const char *name = 0L, const QString &txt = QString::null); KToolBarButton(QWidget *parent = 0L, const char *name = 0L); Methods void on(bool flag = true); QPopupMenu *popup(); virtual void setDefaultIcon(const QString &icon); virtual void setDefaultPixmap(const QPixmap &pixmap); void setDelayedPopup(QPopupMenu *p, bool toggle = false); virtual void setDisabledIcon(const QString &icon); virtual void setDisabledPixmap(const QPixmap &pixmap); void setEnabled(bool enable = true); virtual void setIcon(const QString &icon); virtual void setIcon(const QString &icon, bool generate); void setNoStyle(bool no_style = true); virtual void setPixmap(const QPixmap &pixmap); virtual void setPixmap(const QPixmap &pixmap, bool generate); void setPopup(QPopupMenu *p); void setRadio(bool f = true); virtual void setText(const QString &text); void setToggle(bool toggle = true); void toggle(); Slots void modeChange(); Signals void clicked(int); void doubleClicked(int); void highlighted(int, bool); void pressed(int); void released(int); void toggled(int); 4682-1 ch19.f.qc 11/20/00 15:44 Page 577 578 Part III ✦ Reference and Mechanics Chapter 6 includes examples of creating KToolBarButton widgets inside a KToolBar. KWizard The KWizard widget can be used to create a dialog that guides the user through a sequence of steps. Each step consists of a single window. The KWizard widget pro- vides the paging mechanism and the control buttons. This widget extends QWizard to include a set of standard KDE methods for dialogs. It is the base class on which KDialogBase is built. File #include <kwizard.h> Base Classes QDialog QObject QPaintDevice QWidget QWizard Qt Constructors KWizard(QWidget *parent = 0, const char *name = 0, bool modal = false, WFlags f = 0); The following example displays the empty KWizard widget shown in Figure 19-54: /* showwizard.cpp */ #include <kapp.h> #include <kwizard.h> int main(int argc,char **argv) { QApplication app(argc,argv); KWizard *wizard = new KWizard(); wizard->show(); app.setMainWidget(wizard); return(app.exec()); } Figure 19-54: The KWizard widget Cross- Reference 4682-1 ch19.f.qc 11/20/00 15:44 Page 578 579 Chapter 19 ✦ The Widgets of KDE KXYSelector The KXYSelector widget is a base class that can be used in place of QWidget, and it adds the capability of using the mouse to select a point on the face of the widget. File #include <kselect.h> Base Classes QObject QPaintDevice QWidget Qt Inherited By KHSSelector Constructors KXYSelector(QWidget *parent = 0L, const char *name = 0L); Methods QRect contentsRect(); void setRange(int _minX, int _minY, int _maxX, int _maxY); void setValues(int _xPos, int _yPos); int xValue(); int yValue(); Signals void valueChanged(int _x, int _y); The following example of the KXZWidget displays the cross hairs position indicator shown in Figure 19-55: /* showxyselector.cpp */ #include <kapp.h> #include <kselect.h> int main(int argc,char **argv) { QApplication app(argc,argv); KXYSelector *xyselector = new KXYSelector(); xyselector->show(); app.setMainWidget(xyselector); return(app.exec()); } Figure 19-55: The KXYSelector widget with its cross hairs position indicator 4682-1 ch19.f.qc 11/20/00 15:44 Page 579 580 Part III ✦ Reference and Mechanics Summary This chapter provided an alphabetical listing of each KDE widget. Each widget was listed along with the following: ✦ The constructor, or constructors, that can be used to create instances of the widget ✦ The name of the header file in which the widget is defined ✦ All of the superclasses from which the widget inherits its capabilities ✦ All of the subclasses that derive the capabilities of the widget ✦ The slots and signals that are used to connect an event in one widget to a method call in another ✦ The public methods available to your application This chapter and the previous one include many examples of programs that run in the KDE environment. While there are many differences between these programs and those written for other systems, there are also many similarities. The next chapter contains a point-by-point comparison of the basic structure of a simple KDE program and the same program written for Windows. ✦✦✦ 4682-1 ch19.f.qc 11/20/00 15:44 Page 580 Comparative Anatomy of Windowing Programs I f you are familiar with programming using the Win32 API, this chapter can help you understand the structure of a KDE/Qt application. At the lowest levels, the two program- ming models are very similar. They both operate using a main loop that waits for events to arrive; and when an event does arrive, a function is called to notify the application. To make the comparison as simple as possible, this chapter implements the same short program for both Win32 and KDE. The comparison in this chapter has nothing to do with which windowing system is “better.” Moreover, no attempt is made to use any kind of standard optimal programming techniques. These two programs are contrived to be as much alike as pos- sible, so a person who understands one of them can easily understand the structure of the other. A Win32 Program The following example is a Windows program that fills a win- dow with concentric boxes. Whenever the window resizes, the boxes also resize to fit it. The resulting window looks like the one shown in Figure 20-1. 20 20 CHAPTER ✦✦✦✦ In This Chapter Writing a simple Win32 program that draws squares on a window Writing a simple KDE program that draws squares on a window Writing a simple GNOME program that draws squares on a window Comparing the KDE and Win32 programs point by point ✦✦✦✦ 4682-1 ch20.f.qc 11/13/00 14:16 Page 581 582 Part III ✦ Reference and Mechanics BoxBox 1 /* boxbox.c (win32) */ 2 #include <windows.h> 3 4 #define STEP 3 5 6 static char name[] = “BoxBox”; 7 static int xBox1; 8 static int yBox1; 9 static int xBox2; 10 static int yBox2; 11 12 LRESULT CALLBACK callback(HWND,UINT,WPARAM,LPARAM); 13 14 int WINAPI WinMain(HINSTANCE instance, 15 HINSTANCE prev,PSTR commandLine,int showCommand) 16 { 17 HWND window; 18 MSG message; 19 WNDCLASSEX winclass; 20 21 winclass.cbSize = sizeof (winclass); 22 winclass.style = CS_HREDRAW | CS_VREDRAW; 23 winclass.lpfnWndProc = callback; 24 winclass.cbClsExtra = 0; 25 winclass.cbWndExtra = 0; 26 winclass.hInstance = instance; 27 winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); 28 winclass.hCursor = LoadCursor(NULL,IDC_ARROW); 29 winclass.lpszMenuName = NULL; 30 winclass.lpszClassName = name; 31 winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION); 32 winclass.hbrBackground = 33 (HBRUSH)GetStockObject(WHITE_BRUSH); 34 35 RegisterClassEx(&winclass); 36 window = CreateWindow (name,”Boxes in Boxes”, 37 WS_OVERLAPPEDWINDOW, 38 CW_USEDEFAULT,CW_USEDEFAULT, 39 CW_USEDEFAULT,CW_USEDEFAULT, 40 NULL,NULL,instance,NULL); 41 ShowWindow(window,showCommand); 42 UpdateWindow (window); 43 44 while(GetMessage(&message,NULL,0,0)) { 45 TranslateMessage(&message); 46 DispatchMessage(&message); 47 } 48 return(message.wParam); 49 } 50 51 LRESULT CALLBACK callback(HWND window,UINT messageType, 52 WPARAM wParam,LPARAM lParam) 4682-1 ch20.f.qc 11/13/00 14:16 Page 582 583 Chapter 20 ✦ Comparative Anatomy of Windowing Programs 53 { 54 int x1; 55 int y1; 56 int x2; 57 int y2; 58 HDC hdc; 59 PAINTSTRUCT ps; 60 61 switch (messageType) { 62 case WM_SIZE: 63 xBox1 = 10; 64 yBox1 = 10; 65 xBox2 = LOWORD(lParam) - 10; 66 yBox2 = HIWORD(lParam) - 10; 67 return(0); 68 case WM_PAINT: 69 hdc = BeginPaint(window,&ps); 70 SetViewportOrgEx(hdc,0,0,NULL); 71 x1 = xBox1; 72 x2 = xBox2; 73 y1 = yBox1; 74 y2 = yBox2; 75 while((x1 < x2) && (y1 < y2)) { 76 MoveToEx(hdc,x1,y1,NULL); 77 LineTo(hdc,x2,y1); 78 LineTo(hdc,x2,y2); 79 LineTo(hdc,x1,y2); 80 LineTo(hdc,x1,y1); 81 x1 += STEP; 82 y1 += STEP; 83 x2 -= STEP; 84 y2 -= STEP; 85 } 86 EndPaint(window,&ps); 87 return(0); 88 case WM_DESTROY: 89 PostQuitMessage(0); 90 return(0); 91 } 92 return(DefWindowProc(window,messageType, 93 wParam,lParam)); 94 } Figure 20-1: A Win32 main window 4682-1 ch20.f.qc 11/13/00 14:16 Page 583 584 Part III ✦ Reference and Mechanics The program has only two functions. The function WinMain(), beginning on line 14, is the original one called by the operating system to start the program. The func- tion callback(), on line 51, is called by the operating system whenever an event arrives. A KDE Program The following example is a KDE program that fills a window with concentric boxes. If the size of the window changes, the size of the boxes also changes. The window looks like the one shown in Figure 20-2. The example is in two files: a header file that defines the BoxBox widget, and a C++ source file that contains a mainline for the program as well as the bodies of the methods of the BoxBox class. BoxBox Header 1 /* boxbox.h (KDE) */ 2 #ifndef BOXBOX_H 3 #define BOXBOX_H 4 5 #include <qwidget.h> 6 7 class BoxBox: public QWidget 8 { 9 public: 10 BoxBox(QWidget *parent=0,const char *name=0); 11 private: 12 int xBox1; 13 int yBox1; 14 int xBox2; 15 int yBox2; 16 protected: 17 virtual void paintEvent(QPaintEvent *); 18 virtual void resizeEvent(QResizeEvent *); 19 }; 20 21 #endif BoxBox 1 /* boxbox.cpp (KDE) */ 2 #include <kapp.h> 3 #include <qpainter.h> 4 #include “boxbox.h” 5 6 #define STEP 3 7 8 int main(int argc,char **argv) 9 { 10 KApplication app(argc,argv,”boxbox”); 11 BoxBox boxbox; 4682-1 ch20.f.qc 11/13/00 14:16 Page 584 [...]... checkout checkout checkout checkout checkout checkout checkout checkout checkout checkout checkout checkout kde- qt-addon qt-copy kdelibs kde- i18n kdeadmin kdebase kdegames kdegraphics kdemultimedia kdenetwork kdesdk kdesupport kdetoys kdeutils kdevelop kdoc kfte klyx kmusic koffice korganizer ksite kdepim Keeping Up with Changes You can grab a new copy of the source code whenever you want by downloading... -system-jpeg -no-opengl make 3 Set the KDEDIR environment variable to the installation directory you created in step 1 with a command like this: export KDEDIR=/opt /kde2 4 Compile the kdesupport package with these commands: cp /mnt/cdrom /kde/ kdesupport-2.0.tar.bz2 bunzip2 kdesupport-2.0.tar.bz2 593 4682-1 appA.f.qc 594 11/13/00 14:16 Page 594 Appendixes tar xvf kdsupport-2.0.tar cd kdesupport-2.0 /configure make... gtk_signal_connect(GTK_OBJECT(area),”configure_event”, GTK_SIGNAL_FUNC(eventConfigure),NULL); 4682-1 ch20.f.qc 11/13/00 14:16 Page 5 89 Chapter 20 ✦ Comparative Anatomy of Windowing Programs 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 gtk_widget_show_all(app); gtk_main(); exit(0); } gboolean eventExpose(GtkWidget *widget, GdkEvent... compile the kdelibs package with these commands: cp /mnt/cdrom /kde/ kdelibs-2.0.tar.bz2 bunzip2 kdelibs-2.0.tar.bz2 tar xvf kdlibs-2.0.tar cd kdelibs-2.0 /configure make all make install 6 Finally, using the same sequence of commands as in the previous steps, compile all the rest of the packages in the /mnt/cdrom /kde directory These may be compiled in any order (Note: The packages named kde- i18n- are... Installing KDE 2 from the CD To install the KDE2 from the CD to your hard drive, follow these steps: 1 Create a new directory to hold the KDE installation For example, it could be /usr/local /kde2 , /opt /kde2 , /usr/local /kde, or another location if you prefer 2 In the new directory, enter this sequence of commands to create the qt directory and compile the qt libraries: tar xvzf /mnt/cdrom /kde/ qt-x11-2.2.1.tar... Service phone number: (800) 762- 297 4 Outside the United States, call (317) 572- 399 3 IDG Books will provide technical support only for installation and other general quality control items; for technical support on the applications themselves, consult the program’s vendor or author ✦ ✦ ✦ 595 4682-1 appA.f.qc 11/13/00 14:16 Page 596 4682-1 appB.f.qc 11/13/00 14:16 Page 597 B A P P E N D I X Setting Up... quite handy when programming KDE/ Qt ✦ ✦ ✦ 4682-1 appA.f.qc 11/13/00 14:16 Page 591 A A P P E N D I X What’s on the CD-ROM? T his appendix provides you with information on the contents of the CD-ROM that accompanies this book There are four utility programs included on this CD: ✦ autoconf 2.13 ✦ automake 1.4 ✦ gcc 2 .95 .2 ✦ bzip 1.0.1 source and bzip 1.0.0 binary The source code of KDE 2.0 is on the... large application, it is a good idea KDE and Qt Conveniently, whenever you get a copy of KDE, a matching version of Qt comes with it Because both KDE and Qt are constantly changing, it is important to use the version of Qt that is supplied along with KDE to guarantee compatibility To locate a place to download the software, go to the following Web site: http://www .kde. org/mirrors.html This site provides... the complete source of KDE and all of its standard utilities and programs The utilities directory contains the source of some utilities that are necessary for compiling KDE and KDE applications Electronic version of KDE/ Qt Programming Bible The complete (and searchable) text of this book is on the CD-ROM in Adobe’s Portable Document Format (PDF), readable with the Adobe Acrobat Reader (also included)... version on the CD is 2 .95 .2 To test whether your version is current, enter the following command: gcc version 2 Change to the directory named /usr/local 4682-1 appA.f.qc 11/13/00 14:16 Page 593 Appendix A ✦ What’s on the CD-ROM? 3 Enter the following command, which will create the directory /usr/local/ gcc-2 .95 .2, and fill it with the source files: tar xvzf /mnt/cdrom/utilities/gcc-2 .95 .2.tar.gz 4 Change . this: export KDEDIR=/opt /kde2 4. Compile the kdesupport package with these commands: cp /mnt/cdrom /kde/ kdesupport-2.0.tar.bz2 . bunzip2 kdesupport-2.0.tar.bz2 4682-1 appA.f.qc 11/13/00 14:16 Page 593 594 Appendixes tar. return(FALSE); 88 } 89 gint eventDestroy(GtkWidget *widget, 90 GdkEvent *event,gpointer data) { 91 gtk_main_quit(); 92 return(0); 93 } 4682-1 ch20.f.qc 11/13/00 14:16 Page 5 89 590 Part III ✦ Reference. KWizard(); wizard->show(); app.setMainWidget(wizard); return(app.exec()); } Figure 19- 54: The KWizard widget Cross- Reference 4682-1 ch 19. f.qc 11/20/00 15:44 Page 578 5 79 Chapter 19 ✦ The Widgets of KDE KXYSelector The KXYSelector widget