KDE 2/Qt Programming Bible phần 7 ppsx

74 219 0
KDE 2/Qt Programming Bible phần 7 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

427 Chapter 18 ✦ The Widgets of Qt Enums enum Direction { Horizontal, Vertical }; There are a number of examples of QGrid in Chapter 3. QGroupBox This widget is capable of containing other widgets and providing them with a bor- der and a title. File #include <qgroupbox.h> Base Classes QFrame QObject QPaintDevice QWidget Qt Inherited By QButtonGroup QHButtonGroup QHGroupBox QVButtonGroup QVGroupBox Constructors QGroupBox(QWidget *parent = 0, const char *name = 0); QGroupBox(const QString &title, QWidget *parent = 0, const char *name = 0); QGroupBox(int columns, Orientation o, QWidget *parent = 0, const char *name = 0); QGroupBox(int columns, Orientation o, const QString &title, QWidget *parent = 0, const char *name = 0); Methods void addSpace(int); int alignment() const; int columns() const; Orientation orientation() const; virtual void setAlignment(int); virtual void setColumnLayout(int columns, Orientation o); void setColumns(int); void setOrientation(Orientation); virtual void setTitle(const QString &); QString title() const; The following example uses a QGroupBox widget for its top level window. It has no contents, but it is given a title string, as shown in Figure 18-3. /* showgroupbox.cpp */ #include <qapplication.h> #include <qgroupbox.h> int main(int argc,char **argv) { Cross- Reference 4682-1 ch18.f.qc 11/20/00 15:43 Page 427 428 Part III ✦ Reference and Mechanics QApplication app(argc,argv); QGroupBox *groupbox = new QGroupBox(); groupbox->setTitle(“Group Title”); groupbox->show(); app.setMainWidget(groupbox); return(app.exec()); } Figure 18-3: A QGroupBox as a top-level widget QHBox The QHBox widget is a simple container that organizes widgets side by side. File #include <qhbox.h> Base Classes QFrame QObject QPaintDevice QWidget Qt Inherited By KCharSelect KURLRequester QVBox Constructors QHBox(QWidget *parent = 0, const char *name = 0, WFlags f = 0, bool allowLines = TRUE); Methods void setSpacing(int); bool setStretchFactor(QWidget *, int stretch); QSize sizeHint() const; The following example uses a QHBox as the top-level widget. It has four QLabel wid- gets as its child widgets. As shown in Figure 18-4, each label is displayed side by side, with a 5-pixel-wide space between them. /* showhbox.cpp */ #include <qapplication.h> #include <qhbox.h> #include <qlabel.h> int main(int argc,char **argv) 4682-1 ch18.f.qc 11/20/00 15:43 Page 428 429 Chapter 18 ✦ The Widgets of Qt { QApplication app(argc,argv); QHBox *hbox = new QHBox(); new QLabel(“First”,hbox); new QLabel(“Second”,hbox); new QLabel(“Third”,hbox); new QLabel(“Fourth”,hbox); hbox->setSpacing(5); hbox->show(); app.setMainWidget(hbox); return(app.exec()); } Figure 18-4: Labels displayed by a QHBox QHButtonGroup The QHButtonGroup is a container widget that organizes a collection of buttons in a horizontal row. File #include <qhbuttongroup.h> Base Classes QButtonGroup QFrame QGroupBox QObject QPaintDevice QWidget Qt Constructors QHButtonGroup(QWidget *parent = 0, const char *name = 0); QHButtonGroup(const QString &title, QWidget *parent = 0, const char *name = 0); You can find example programs that use QHButtonGroup in Chapter 7. QHeader The QHeader widget is a container that controls the size and position of a number of column headings. File #include <qheader.h> Base Classes QObject QPaintDevice QWidget Qt Cross- Reference 4682-1 ch18.f.qc 11/20/00 15:43 Page 429 430 Part III ✦ Reference and Mechanics Constructors QHeader(QWidget *parent = 0, const char *name = 0); QHeader(int, QWidget *parent = 0, const char *name = 0); Methods int addLabel(const QString &, int size = - 1); int addLabel(const QIconSet &, const QString &, int size = - 1); int cellAt(int) const; int cellPos(int) const; int cellSize(int) const; int count() const; QIconSet *iconSet(int section) const; bool isClickEnabled(int section = - 1) const; bool isMovingEnabled() const; bool isResizeEnabled(int section = - 1) const; QString label(int section) const; int mapToActual(int) const; int mapToIndex(int section) const; int mapToLogical(int) const; int mapToSection(int index) const; virtual void moveCell(int, int); void moveSection(int section, int toIndex); int offset() const; Orientation orientation() const; void removeLabel(int section); void resizeSection(int section, int s); int sectionAt(int pos) const; int sectionPos(int section) const; int sectionSize(int section) const; virtual void setCellSize(int, int); virtual void setClickEnabled(bool, int section = - 1); virtual void setLabel(int, const QString &, int size = - 1); virtual void setLabel(int, const QIconSet &, const QString &, int size = - 1); virtual void setMovingEnabled(bool); virtual void setOrientation(Orientation); virtual void setResizeEnabled(bool, int section = - 1); void setSortIndicator(int section, bool increasing = TRUE); virtual void setTracking(bool enable); QSize sizeHint() const; QSizePolicy sizePolicy() const; bool tracking() const; Slots virtual void setOffset(int pos); Signals void clicked(int section); void indexChange(int section, int fromIndex, int toIndex); void moved(int, int); void pressed(int section); 4682-1 ch18.f.qc 11/20/00 15:43 Page 430 431 Chapter 18 ✦ The Widgets of Qt void released(int section); void sectionClicked(int); void sizeChange(int section, int oldSize, int newSize); The following example creates a QHeader with four columns, with text of differing lengths. The size of each column head can be adjusted by using the mouse. The head- ings all maintain the width assigned to them, even if some of the text is obscured. As shown in Figure 18-5, the header may extend beyond the right end of the window, and one header label may be sized to overlap another. Column headings can also be expanded beyond the size required to display the text. A group of signals issued by the QHeader widget can be used to maintain the size and status of the columns beneath the headings. /* showheader.cpp */ #include <qapplication.h> #include <qheader.h> int main(int argc,char **argv) { QApplication app(argc,argv); QHeader *header = new QHeader(); header->addLabel(“Column One”); header->addLabel(“Two”); header->addLabel(“Three”); header->addLabel(“Fourth Column”); header->show(); app.setMainWidget(header); return(app.exec()); } Figure 18-5: A QHeader widget containing four column headings QHGroupBox The QHGroupBox is a container widget that organizes a collection of widgets in a horizontal row. File #include <qhgroupbox.h> Base Classes QFrame QGroupBox QObject QPaintDevice QWidget Qt Constructors QHGroupBox(QWidget *parent = 0, const char *name = 0); QHGroupBox(const QString &title, QWidget *parent = 0, const char *name = 0); 4682-1 ch18.f.qc 11/20/00 15:43 Page 431 432 Part III ✦ Reference and Mechanics The following example contains four labels inside a QHGroupBox widget. As shown in Figure 18-6, the QHGroupBox widget inherits from QFrame, so it displays a border around the contained widgets, and can optionally display a title. /* showhgroupbox.cpp */ #include <qapplication.h> #include <qhgroupbox.h> #include <qlabel.h> int main(int argc,char **argv) { QApplication app(argc,argv); QHGroupBox *hgroupbox = new QHGroupBox(); new QLabel(“First”,hgroupbox); new QLabel(“Second”,hgroupbox); new QLabel(“Third”,hgroupbox); new QLabel(“Fourth”,hgroupbox); hgroupbox->setTitle(“Group Box Title”); hgroupbox->show(); app.setMainWidget(hgroupbox); return(app.exec()); } Figure 18-6: Four buttons contained by a QHGroupBox QIconView The QIconView widget displays a collection of icons and enables the user to make a selection. File #include <qiconview.h> Base Classes QFrame QObject QPaintDevice QScrollView QWidget Qt Inherited By KFileIconView KIconCanvas KIconView Constructors QIconView(QWidget *parent = 0, const char *name = 0, WFlags f = 0); Methods Arrangement arrangement() const; bool autoArrange() const; virtual void clear(); 4682-1 ch18.f.qc 11/20/00 15:43 Page 432 433 Chapter 18 ✦ The Widgets of Qt virtual void clearSelection(); uint count() const; QIconViewItem *currentItem() const; void ensureItemVisible(QIconViewItem *item); bool eventFilter(QObject *o, QEvent *); QIconViewItem *findFirstVisibleItem(const QRect &r) const; QIconViewItem *findItem(const QPoint &pos) const; QIconViewItem *findItem(const QString &text) const; QIconViewItem *findLastVisibleItem(const QRect &r) const; QIconViewItem *firstItem() const; int gridX() const; int gridY() const; int index(const QIconViewItem *item) const; virtual void insertItem(QIconViewItem *item, QIconViewItem *after = 0L); virtual void invertSelection(); QBrush itemTextBackground() const; ItemTextPos itemTextPos() const; bool itemsMovable() const; QIconViewItem *lastItem() const; int maxItemTextLength() const; int maxItemWidth() const; QSize minimumSizeHint() const; virtual void repaintItem(QIconViewItem *item); ResizeMode resizeMode() const; virtual void selectAll(bool select); SelectionMode selectionMode() const; virtual void setArrangement(Arrangement am); virtual void setAutoArrange(bool b); virtual void setCurrentItem(QIconViewItem *item); virtual void setFont(const QFont &); virtual void setGridX(int rx); virtual void setGridY(int ry); virtual void setItemTextBackground(const QBrush &b); virtual void setItemTextPos(ItemTextPos pos); virtual void setItemsMovable(bool b); virtual void setMaxItemTextLength(int w); virtual void setMaxItemWidth(int w); virtual void setPalette(const QPalette &); virtual void setResizeMode(ResizeMode am); virtual void setSelected(QIconViewItem *item, bool s, bool cb = FALSE); virtual void setSelectionMode(SelectionMode m); virtual void setShowToolTips(bool b); void setSorting(bool sort, bool ascending = TRUE); virtual void setSpacing(int sp); virtual void setWordWrapIconText(bool b); virtual void showEvent(QShowEvent *); bool showToolTips() const; QSize sizeHint() const; QSizePolicy sizePolicy() const; virtual void sort(bool ascending = TRUE); bool sortDirection() const; bool sorting() const; 4682-1 ch18.f.qc 11/20/00 15:43 Page 433 434 Part III ✦ Reference and Mechanics int spacing() const; virtual void takeItem(QIconViewItem *item); bool wordWrapIconText() const; Slots virtual void arrangeItemsInGrid(const QSize &grid, bool update = TRUE); virtual void arrangeItemsInGrid(bool update = TRUE); virtual void setContentsPos(int x, int y); virtual void updateContents(); Signals void clicked(QIconViewItem *); void clicked(QIconViewItem *, const QPoint &); void currentChanged(QIconViewItem *item); void doubleClicked(QIconViewItem *item); void dropped(QDropEvent *e, const QValueList < QIconDragItem > &lst); void itemRenamed(QIconViewItem *item, const QString &); void itemRenamed(QIconViewItem *item); void mouseButtonClicked(int button, QIconViewItem *item, const QPoint &pos); void mouseButtonPressed(int button, QIconViewItem *item, const QPoint &pos); void moved(); void onItem(QIconViewItem *item); void onViewport(); void pressed(QIconViewItem *); void pressed(QIconViewItem *, const QPoint &); void returnPressed(QIconViewItem *item); void rightButtonClicked(QIconViewItem *item, const QPoint &pos); void rightButtonPressed(QIconViewItem *item, const QPoint &pos); void selectionChanged(); void selectionChanged(QIconViewItem *item); Enums enum SelectionMode { Single=0, Multi, Extended, NoSelection }; enum Arrangement { LeftToRight=0, TopToBottom }; enum ResizeMode { Fixed=0, Adjust }; enum ItemTextPos { Bottom=0, Right }; The following example displays the five icons shown in Figure 18-7. The first icon has no pixmap and no text, so it uses the default pixmap and has no label. The next two icons also use the default pixmap, but they both have text for labels. The last two icons have both pixmaps and labels, and the icon labeled “Flag” has been selected by the mouse. /* showiconview.cpp */ #include <qapplication.h> #include <qiconview.h> int main(int argc,char **argv) 4682-1 ch18.f.qc 11/20/00 15:43 Page 434 435 Chapter 18 ✦ The Widgets of Qt { QIconViewItem *item; QApplication app(argc,argv); QIconView *iconview = new QIconView(); item = new QIconViewItem(iconview); item = new QIconViewItem(iconview,”Icon Label”); item = new QIconViewItem(iconview,”Icon With\nLong Label”); QPixmap flag(“flag.png”); item = new QIconViewItem(iconview,”Flag”,flag); QPixmap idea(“idea.png”); item = new QIconViewItem(iconview,”Idea”,idea); iconview->show(); app.setMainWidget(iconview); return(app.exec()); } Figure 18-7: A QIconView widget displaying five icons QInputDialog The QInputDialog widget is a collection of static methods, each of which pops up a dialog that prompts the user for input. File #include <qinputdialog.h> Base Classes QDialog QObject QPaintDevice QWidget Qt Methods static double getDouble(const QString &caption, const QString &label, double num = 0, double from = - 2147483647, double to = 2147483647, int step = 1, bool *ok = 0, QWidget *parent = 0, const char *name = 0); static int getInteger(const QString &caption, const QString &label, int num = 0, int from = - 2147483647, int to = 2147483647, int step = 1, bool *ok = 0, QWidget *parent = 0, const char *name = 0); static QString getItem(const QString &caption, const QString &label, const QStringList &list, int current = 0, bool editable = TRUE, bool *ok = 0, QWidget *parent = 0, const char *name = 0); 4682-1 ch18.f.qc 11/20/00 15:43 Page 435 436 Part III ✦ Reference and Mechanics static QString getText(const QString &caption, const QString &label, const QString &text = QString::null, bool *ok = 0, QWidget *parent = 0, const char *name = 0); The following example prompts the user for a double value from 1.0 to 10.0. If the user selects the OK button, the Boolean value OK will be set to true; other- wise, it will be set to false. As shown in Figure 18-8, the arguments passed to getDouble() also set the caption at the top of the window and display a prompt immediately above the text window. /* showcolordialog.cpp */ #include <qapplication.h> #include <qinputdialog.h> #include <iostream.h> int main(int argc,char **argv) { bool OK; QApplication app(argc,argv); double value = QInputDialog::getDouble( “A Double Value”, “Enter a number from 1.0 to 10.0”, 8.902, 1.0,10.0, 1,&OK); if(OK) cout << “The value is: “ << value << endl; else cout << “No data entered.” << endl; return(app.exec()); } Figure 18-8: A QInputDialog dialog prompting for a double value QLCDNumber The QLCDNumber widget displays a number using a font that looks like the digits of an LCD display. File #include <qlcdnumber.h> 4682-1 ch18.f.qc 11/20/00 15:43 Page 436 [...]... defaultButtonNumber = 0, int escapeButtonNumber = - 1); Enums enum Icon { NoIcon=0, Information=1, Warning=2, Critical=3 }; enum (anon) { Ok=1, Cancel=2, Yes=3, No=4, Abort=5, Retry=6, Ignore =7, ButtonMask=0x 07, Default=0x100, Escape=0x200, FlagMask=0x300 }; 4682-1 ch18.f.qc 11/20/00 15:43 Page 451 Chapter 18 ✦ The Widgets of Qt The following example uses one of the static methods to pop up a preconfigured... SegmentStyle { Outline, Filled, Flat }; You can find an example of the QLCDNumber widget in Chapter 3 CrossReference QLabel The QLabel widget displays unadorned text in any font File #include 4 37 4682-1 ch18.f.qc 438 11/20/00 15:43 Page 438 Part III ✦ Reference and Mechanics Base Classes QFrame QObject QPaintDevice QWidget Qt Inherited By KDockWindow KImageTrackLabel KStatusBarLabel KURLLabel... eventFilter(QObject *, QEvent *); bool getLocation(QToolBar *tb, ToolBarDock &dock, int &index, bool &nl, int &extraOffset) const; bool isDockEnabled(ToolBarDock dock) const; 4682-1 ch18.f.qc 11/20/00 15:43 Page 4 47 Chapter 18 ✦ The Widgets of Qt bool isDockEnabled(QToolBar *tb, ToolBarDock dock) const; bool isDockMenuEnabled() const; void lineUpToolBars(bool keepNewLines = FALSE); QMenuBar *menuBar() const; QSize... objects It also contains a QWidget at its center, which is intended to become the main display window of your application /* showmainwindow.cpp */ #include #include 4 47 4682-1 ch18.f.qc 448 11/20/00 15:43 Page 448 Part III ✦ Reference and Mechanics int main(int argc,char **argv) { QApplication app(argc,argv); QMainWindow *mainwindow = new QMainWindow(); mainwindow->show();...4682-1 ch18.f.qc 11/20/00 15:43 Page 4 37 Chapter 18 ✦ The Widgets of Qt Base Classes QFrame QObject QPaintDevice QWidget Qt Constructors QLCDNumber(QWidget *parent = 0, const char *name = 0); QLCDNumber(uint numDigits, QWidget *parent = 0,... QProgressBar is a horizontal progress bar with a Cancel button Note that this is a widget, not a pop-up window that inherits from QDialog File #include 4682-1 ch18.f.qc 11/20/00 15:43 Page 4 57 Chapter 18 ✦ The Widgets of Qt Base Classes QObject QPaintDevice QSemiModal QWidget Qt Constructors QProgressDialog(QWidget *parent = 0, const char *name = 0, bool modal = FALSE, WFlags f = 0); QProgressDialog(const... argc,char **argv) { QApplication app(argc,argv); QProgressDialog *progressdialog = new QProgressDialog(); progressdialog->setTotalSteps(200); progressdialog->setProgress(122); progressdialog->show(); 4 57 4682-1 ch18.f.qc 458 11/20/00 15:43 Page 458 Part III ✦ Reference and Mechanics app.setMainWidget(progressdialog); return(app.exec()); } Figure 18-14: A QProgressDialog showing the progress and a Cancel... char *name = 0); Methods bool isChecked() const; virtual void setChecked(bool check); QSize sizeHint() const; QSizePolicy sizePolicy() const; There are a number of examples of the QRadioButton in Chapter 7 CrossReference QScrollBar The QScrollBar widget can be configured as either a vertical or horizontal scrollbar File #include Base Classes QObject QPaintDevice QRangeControl QWidget Qt . QString &caption, const QString &label, double num = 0, double from = - 21 474 836 47, double to = 21 474 836 47, int step = 1, bool *ok = 0, QWidget *parent = 0, const char *name = 0); static. getInteger(const QString &caption, const QString &label, int num = 0, int from = - 21 474 836 47, int to = 21 474 836 47, int step = 1, bool *ok = 0, QWidget *parent = 0, const char *name = 0); static. *parent = 0, const char *name = 0); You can find example programs that use QHButtonGroup in Chapter 7. QHeader The QHeader widget is a container that controls the size and position of a number of

Ngày đăng: 13/08/2014, 22:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan