q3table.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 2,173 行 · 第 1/5 页

CPP
2,173
字号
    Rows and columns can be set to be movable using    rowMovingEnabled() and columnMovingEnabled(). The user can drag    them to reorder them holding down the Ctrl key and dragging the    mouse. For performance reasons, the default behavior of Q3Header    section numbers is overridden by Q3Table. Currently in Q3Table, when    a row or column is dragged and reordered, the section number is    also changed to its new position. Therefore, there is no    difference between the section and the index fields in Q3Header.    The Q3Table Q3Header classes do not provide a mechanism for indexing    independently of the user interface ordering.    The table can be sorted using sortColumn(). Users can sort a    column by clicking its header if setSorting() is set to true. Rows    can be swapped with swapRows(), columns with swapColumns() and    cells with swapCells().    For editable tables (see setReadOnly()) you can set the read-only    property of individual rows and columns with setRowReadOnly() and    setColumnReadOnly(). (Whether a cell is editable or read-only    depends on these settings and the cell's Q3TableItem.    The row and column which have the focus are returned by    currentRow() and currentColumn() respectively.    Although many Q3Table functions operate in terms of rows and    columns the indexOf() function returns a single integer    identifying a particular cell.    \target cells    \section1 Cells    All of a Q3Table's cells are empty when the table is constructed.    There are two approaches to populating the table's cells. The    first and simplest approach is to use Q3TableItems or Q3TableItem    subclasses. The second approach doesn't use Q3TableItems at all    which is useful for very large sparse tables but requires you to    reimplement a number of functions. We'll look at each approach in    turn.    To put a string in a cell use setText(). This function will create    a new Q3TableItem for the cell if one doesn't already exist, and    displays the text in it. By default the table item's widget will    be a QLineEdit. A pixmap may be put in a cell with setPixmap(),    which also creates a table item if required. A cell may contain \e    both a pixmap and text; the pixmap is displayed to the left of the    text. Another approach is to construct a Q3TableItem or Q3TableItem    subclass, set its properties, then insert it into a cell with    setItem().    If you want cells which contain comboboxes use the Q3ComboTableItem    class. Similarly if you require cells containing checkboxes use    the Q3CheckTableItem class. These table items look and behave just    like the combobox or checkbox widgets but consume far less memory.    Q3Table takes ownership of its Q3TableItems and will delete them    when the table itself is destroyed. You can take ownership of a    table item using takeItem() which you use to move a cell's    contents from one cell to another, either within the same table,    or from one table to another. (See also, swapCells()).    In-place editing of the text in Q3TableItems, and the values in    Q3ComboTableItems and Q3CheckTableItems works automatically. Cells    may be editable or read-only, see Q3TableItem::EditType. If you    want fine control over editing see beginEdit() and endEdit().    The contents of a cell can be retrieved as a Q3TableItem using    item(), or as a string with text() or as a pixmap (if there is    one) with pixmap(). A cell's bounding rectangle is given by    cellGeometry(). Use updateCell() to repaint a cell, for example to    clear away a cell's visual representation after it has been    deleted with clearCell(). The table can be forced to scroll to    show a particular cell with ensureCellVisible(). The isSelected()    function indicates if a cell is selected.    It is possible to use your own widget as a cell's widget using    setCellWidget(), but subclassing Q3TableItem might be a simpler    approach. The cell's widget (if there is one) can be removed with    clearCellWidget().    \keyword notes on large tables    \target bigtables    \section2 Large tables    For large, sparse, tables using Q3TableItems or other widgets is    inefficient. The solution is to \e draw the cell as it should    appear and to create and destroy cell editors on demand.    This approach requires that you reimplement various functions.    Reimplement paintCell() to display your data, and createEditor()    and setCellContentFromEditor() to support in-place editing. It    is very important to reimplement resizeData() to have no    functionality, to prevent Q3Table from attempting to create a huge    array. You will also need to reimplement item(), setItem(),    takeItem(), clearCell(), and insertWidget(), cellWidget() and    clearCellWidget(). In almost every circumstance (for sorting,    removing and inserting columns and rows, etc.), you also need    to reimplement swapRows(), swapCells() and swapColumns(), including    header handling.    If you represent active cells with a dictionary of Q3TableItems and    QWidgets, i.e. only store references to cells that are actually    used, many of the functions can be implemented with a single line    of code.    For more information on cells see the Q3TableItem documenation.    \target selections    \section1 Selections    Q3Table's support single selection, multi-selection (multiple    cells) or no selection. The selection mode is set with    setSelectionMode(). Use isSelected() to determine if a particular    cell is selected, and isRowSelected() and isColumnSelected() to    see if a row or column is selected.    Q3Table's support many simultaneous selections. You can    programmatically select cells with addSelection(). The number of    selections is given by numSelections(). The current selection is    returned by currentSelection(). You can remove a selection with    removeSelection() and remove all selections with    clearSelection(). Selections are Q3TableSelection objects.    To easily add a new selection use selectCells(), selectRow() or    selectColumn().    Alternatively, use addSelection() to add new selections using    Q3TableSelection objects. The advantage of using Q3TableSelection    objects is that you can call Q3TableSelection::expandTo() to resize    the selection and can query and compare them.    The number of selections is given by numSelections(). The current    selection is returned by currentSelection(). You can remove a    selection with removeSelection() and remove all selections with    clearSelection().    \target signals    \section1 Signals    When the user clicks a cell the currentChanged() signal is    emitted. You can also connect to the lower level clicked(),    doubleClicked() and pressed() signals. If the user changes the    selection the selectionChanged() signal is emitted; similarly if    the user changes a cell's value the valueChanged() signal is    emitted. If the user right-clicks (or presses the appropriate    platform-specific key sequence) the contextMenuRequested() signal    is emitted. If the user drops a drag and drop object the dropped()    signal is emitted with the drop event.*//*!    \fn void Q3Table::currentChanged(int row, int col)    This signal is emitted when the current cell has changed to \a    row, \a col.*//*!    \fn void Q3Table::valueChanged(int row, int col)    This signal is emitted when the user changed the value in the cell    at \a row, \a col.*//*!    \fn int Q3Table::currentRow() const    Returns the current row.    \sa currentColumn()*//*!    \fn int Q3Table::currentColumn() const    Returns the current column.    \sa currentRow()*//*!    \enum Q3Table::EditMode    \value NotEditing  No cell is currently being edited.    \value Editing  A cell is currently being edited. The editor was    initialised with the cell's contents.    \value Replacing  A cell is currently being edited. The editor was    not initialised with the cell's contents.*//*!    \enum Q3Table::SelectionMode    \value NoSelection No cell can be selected by the user.    \value Single The user may only select a single range of cells.    \value Multi The user may select multiple ranges of cells.    \value SingleRow The user may select one row at once.    \value MultiRow The user may select multiple rows.*//*!    \enum Q3Table::FocusStyle    Specifies how the current cell (focus cell) is drawn.    \value FollowStyle The current cell is drawn according to the    current style and the cell's background is also drawn selected, if    the current cell is within a selection    \value SpreadSheet The current cell is drawn as in a spreadsheet.    This means, it is signified by a black rectangle around the cell,    and the background of the current cell is always drawn with the    widget's base color - even when selected.*//*!    \fn void Q3Table::clicked(int row, int col, int button, const QPoint &mousePos)    This signal is emitted when mouse button \a button is clicked. The    cell where the event took place is at \a row, \a col, and the    mouse's position is in \a mousePos.    \sa Qt::MouseButton*//*!    \fn void Q3Table::doubleClicked(int row, int col, int button, const QPoint &mousePos)    This signal is emitted when mouse button \a button is    double-clicked. The cell where the event took place is at \a row,    \a col, and the mouse's position is in \a mousePos.    \sa Qt::MouseButton*//*!    \fn void Q3Table::pressed(int row, int col, int button, const QPoint &mousePos)    This signal is emitted when mouse button \a button is pressed. The    cell where the event took place is at \a row, \a col, and the    mouse's position is in \a mousePos.    \sa Qt::MouseButton*//*!    \fn void Q3Table::selectionChanged()    This signal is emitted whenever a selection changes.    \sa Q3TableSelection*//*!    \fn void Q3Table::contextMenuRequested(int row, int col, const QPoint & pos)    This signal is emitted when the user invokes a context menu with    the right mouse button (or with a system-specific keypress). The    cell where the event took place is at \a row, \a col. \a pos is    the position where the context menu will appear in the global    coordinate system. This signal is always emitted, even if the    contents of the cell are disabled.*//*!    Creates an empty table object called \a name as a child of \a    parent.    Call setNumRows() and setNumCols() to set the table size before    populating the table if you're using Q3TableItems.*/Q3Table::Q3Table(QWidget *parent, const char *name)    : Q3ScrollView(parent, name, WNoAutoErase | WStaticContents),      leftHeader(0), topHeader(0),      currentSel(0), lastSortCol(-1), sGrid(true), mRows(false), mCols(false),      asc(true), doSort(true), readOnly(false){    init(0, 0);}/*!    Constructs an empty table called \a name with \a numRows rows and    \a numCols columns. The table is a child of \a parent.    If you're using \l{Q3TableItem}s to populate the table's cells, you    can create Q3TableItem, Q3ComboTableItem and Q3CheckTableItem items    and insert them into the table using setItem(). (See the notes on    large tables for an alternative to using Q3TableItems.)*/Q3Table::Q3Table(int numRows, int numCols, QWidget *parent, const char *name)    : Q3ScrollView(parent, name, WNoAutoErase | WStaticContents),      leftHeader(0), topHeader(0),      currentSel(0), lastSortCol(-1), sGrid(true), mRows(false), mCols(false),      asc(true), doSort(true), readOnly(false){    init(numRows, numCols);}/*! \internal*/void Q3Table::init(int rows, int cols){#ifndef QT_NO_DRAGANDDROP    setDragAutoScroll(false);#endif    d = new Q3TablePrivate;    d->geomTimer = new QTimer(this);    d->lastVisCol = 0;    d->lastVisRow = 0;    connect(d->geomTimer, SIGNAL(timeout()), this, SLOT(updateGeometriesSlot()));    shouldClearSelection = false;    dEnabled = false;    roRows.setAutoDelete(true);    roCols.setAutoDelete(true);    setSorting(false);    unused = true; // It's unused, ain't it? :)    selMode = Multi;    contents.setAutoDelete(true);    widgets.setAutoDelete(true);    // Enable clipper and set background mode    enableClipper(qt_table_clipper_enabled);    viewport()->setFocusProxy(this);    viewport()->setFocusPolicy(Qt::WheelFocus);    setFocusPolicy(Qt::WheelFocus);    viewport()->setBackgroundMode(PaletteBase);    setBackgroundMode(PaletteBackground, PaletteBase);    setResizePolicy(Manual);    selections.setAutoDelete(true);    // Create headers    leftHeader = new Q3TableHeader(rows, this, this, "left table header");    leftHeader->setOrientation(Vertical);    leftHeader->setTracking(true);    leftHeader->setMovingEnabled(true);    topHeader = new Q3TableHeader(cols, this, this, "right table header");    topHeader->setOrientation(Horizontal);    topHeader->setTracking(true);    topHeader->setMovingEnabled(true);    if (QApplication::reverseLayout())        setMargins(0, fontMetrics().height() + 4, 30, 0);    else        setMargins(30, fontMetrics().height() + 4, 0, 0);    topHeader->setUpdatesEnabled(false);    leftHeader->setUpdatesEnabled(false);    // Initialize headers    int i = 0;    for (i = 0; i < numCols(); ++i)        topHeader->resizeSection(i, QMAX(100, QApplication::globalStrut().height()));    for (i = 0; i < numRows(); ++i)        leftHeader->resizeSection(i, QMAX(20, QApplication::globalStrut().width()));    topHeader->setUpdatesEnabled(true);    leftHeader->setUpdatesEnabled(true);    // Prepare for contents    contents.setAutoDelete(false);    // Connect header, table and scroll bars    connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),             topHeader, SLOT(setOffset(int)));    connect(verticalScrollBar(), SIGNAL(valueChanged(int)),             leftHeader, SLOT(setOffset(int)));    connect(topHeader, SIGNAL(sectionSizeChanged(int)),             this, SLOT(columnWidthChanged(int)));    connect(topHeader, SIGNAL(indexChange(int,int,int)),             this, SLOT(columnIndexChanged(int,int,int)));    connect(topHeader, SIGNAL(sectionClicked(int)),             this, SLOT(columnClicked(int)));    connect(leftHeader, SIGNAL(sectionSizeChanged(int)),             this, SLOT(rowHeightChanged(int)));    connect(leftHeader, SIGNAL(indexChange(int,int,int)),             this, SLOT(rowIndexChanged(int,int,int)));    // Initialize variables    autoScrollTimer = new QTimer(this);    connect(autoScrollTimer, SIGNAL(timeout()),             this, SLOT(doAutoScroll()));    curRow = curCol = 0;    topHeader->setSectionState(curCol, Q3TableHeader::Bold);    leftHeader->setSectionState(curRow, Q3TableHeader::Bold);    edMode = NotEditing;    editRow = editCol = -1;    drawActiveSelection = true;    installEventFilter(this);    focusStl = SpreadSheet;    was_visible = false;    // initial size    resize(640, 480);}/*!    Releases all the resources used by the Q3Table object,    including all \l{Q3TableItem}s and their widgets.*/Q3Table::~Q3Table(){    setUpdatesEnabled(false);    contents.setAutoDelete(true);    contents.clear();    widgets.clear();    delete d;}void Q3Table::setReadOnly(bool b){    readOnly = b;    Q3TableItem *i = item(curRow, curCol);    if (readOnly && isEditing()) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?