彩票走势图

Qt网格组件DataGrid:如何在网格中输入新行

翻译|使用教程|编辑:鲍佳佳|2020-11-16 11:32:29.477|阅读 132 次

概述:QtitanDataGrid是一款适用于Qt的商业化DataGrid 组件,它使得表格数据可以直接面向终端用户。本文演示了用户如何在网格中输入新行。新行可以位于视图的顶部或底部。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

QtitanDataGrid是一款适用于Qt的商业化DataGrid 组件,它使得表格数据可以直接面向终端用户。这个组件吸收了用户界面结构显示表格方面所有的现代化技术的精华,是目前Qt市场上唯一一款拥有如此高级功能和出色性能的网格组件。这个Qt数据网格组件使用纯C++创建,运行速度极快,处理大数据和超大数据集的效果突出。QtitanDataGrid完全集成了QtDesigner,因而极易适应其他相似的开发环境,保证100%兼容Qt GUI。

点击下载QtitanDataGrid最新试用版

该示例显示了如何使用添加新行功能。

该示例演示了用户如何在网格中输入新行。新行可以位于视图的顶部或底部。


代码如下:

.pro文件:

TEMPLATE = app

TARGET = "Grid_"$$member(TARGET, 0)

QTITANDIR = $$quote($$(QTITANDIR))
isEmpty(QTITANDIR):QTITANDIR = $$quote($$PWD/../../../)
include($$QTITANDIR/src/shared/qtitangrid.pri)

DESTDIR = $$QTITANDIR/bin
DESTDIR = $$member(DESTDIR, 0)$$QTITAN_LIB_PREFIX
DESTDIR = "$$DESTDIR"

android:ANDROID_PACKAGE_SOURCE_DIR=$$PWD/../../SQLFiles

!debug_and_release|build_pass {
    CONFIG(debug, debug|release) {
        TARGET = $$member(TARGET, 0)d
    }
}

HEADERS      += window.h

SOURCES      += main.cpp \
                window.cpp

include($$PWD/../../shared/DemoMainWindow.pri)

QT += widgets sql xml

macx {
    CONFIG-=app_bundle
}

QMAKE_CXXFLAGS -= FS

.py文件:

import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../../shared")

from PySide2 import QtCore
from PySide2.QtCore import Qt, SIGNAL, SLOT, QTimer
from PySide2.QtGui import QPixmap
from PySide2.QtWidgets import (QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton,
                               QSlider, QLabel, QCheckBox, QComboBox, QMessageBox)
from PySide2.QtSql import QSqlDatabase, QSqlError, QSqlTableModel

from DevMachines import QtitanBase
from DevMachines.QtitanBase import Qtitan
from DevMachines.QtitanGrid import (getGridVersion, Grid, DBGrid, GridColumn, GridEditor,
                                    CellButtonClickEventArgs, ContextMenuEventArgs,
                                    EditorValidationEventArgs)

from DemoMainWindow import DemoMainWindow

class Window(DemoMainWindow):
    def __init__(self):
        DemoMainWindow.__init__(self, "QtitanDataGrid", getGridVersion())
        self.setWindowTitle(self.tr("Adding row to the grid possibility"))
        self.setGeometry(150, 150, 1000, 800)

        Grid.loadTranslation()

        self.grid = DBGrid()

        prefix = os.path.dirname(os.path.realpath(__file__))
        prefix += "/../../SQLFiles/assets"

        db = QSqlDatabase.addDatabase("QSQLITE", "database_demo")

        db.setDatabaseName(prefix + "/database.sqlite")
        db.setHostName("")
        db.setPort(-1)
        if not db.open("", ""):
            err = db.lastError()
            QSqlDatabase.removeDatabase("database_demo")
            QMessageBox.critical(self, "Demo Error", "Error: Can't open database " + db.databaseName() + ", error - " + err.text())
            QApplication.exit(1)
            return

        model = QSqlTableModel(self.grid, db)
        model.setTable("data")
        model.select()
        if model.lastError().type() != QSqlError.NoError:
            QMessageBox.critical(0, "Demo Error", "Error: SQL data base is not valid.")
            QApplication.exit(1)
            return

        model.setEditStrategy(QSqlTableModel.OnFieldChange)

        # Configure grid view
        self.grid.setViewType(Grid.BandedTableView)
        view = self.grid.view()
        view.options().setBandsHeader(False)
        view.options().setColumnAutoWidth(True)
        view.options().setRowAutoHeight(True)
        view.options().setNewRowPlace(Qtitan.AtBeginning)
        view.options().setNewRowHighlightEffect(Qtitan.AlphaEffect)

        # Connect Grid's context menu handler.
        self.connect(view, SIGNAL("contextMenu(ContextMenuEventArgs*)"), self,
                SLOT("contextMenu(ContextMenuEventArgs*)"))

        characteristicsBand = view.addBand("Characteristics")
        engineeringBand = view.addBand("Engineering")
        view.setModel(model)

        column = view.getColumnByModelColumnName("Photo")
        column = view.getColumnByModelColumnName("Registration")
        column.setBandIndex(characteristicsBand.index())
        column = view.getColumnByModelColumnName("Aircraft")
        column.setBandIndex(characteristicsBand.index())
        column = view.getColumnByModelColumnName("Location")
        column.setBandIndex(characteristicsBand.index())
        column.setRowIndex(1)
        column = view.getColumnByModelColumnName("Date")
        column.setBandIndex(characteristicsBand.index())
        column.setRowIndex(2)
        # Add cell button to the column.
        column.addButton(GridColumn.ClearButtonIcon, Qtitan.AtEnd, GridColumn.MouseOverPolicy)
        self.connect(column, SIGNAL("buttonClicked(CellButtonClickEventArgs*)"), self,
                    SLOT("cellButtonClicked(CellButtonClickEventArgs*)"))

        column = view.getColumnByModelColumnName("Photo")
        column.setEditorType(GridEditor.Picture)
        pictureEditor = column.editorRepository()

        column.setBandIndex(engineeringBand.index())
        column.setRowSpan(3)

        column = view.getColumnByModelColumnName("History")
        column.setEditorType(GridEditor.Memo)
        column.setBandIndex(engineeringBand.index())
        column.setRowSpan(3)

        # Add cell button to the column.
        column.addButton(GridColumn.ChoiceButtonIcon, Qtitan.AtEnd)
        self.connect(column, SIGNAL("buttonClicked(CellButtonClickEventArgs*)"), self,
                SLOT("cellButtonClicked(CellButtonClickEventArgs*)"))

        column = view.getColumnByModelColumnName("Info")
        column.setEditorType(GridEditor.Memo)
        column.setBandIndex(engineeringBand.index())
        column.setRowSpan(3)

        # Add cell button to the column.
        column.addButton(GridColumn.ChoiceButtonIcon, Qtitan.AtEnd)
        self.connect(column, SIGNAL("buttonClicked(CellButtonClickEventArgs*)"), self,
                SLOT("cellButtonClicked(CellButtonClickEventArgs*)"))

        # Show button menu for all column headers.
        for i in range(0, view.getColumnCount()):
            view.getColumn(i).setMenuButtonVisible(True)

        self.setDemoWidget(self.grid, self.createSettingsWidget())
        view.bestFit()

    def createSettingsWidget(self):
        settings = QWidget(self)
        l = QVBoxLayout(settings)
        placeLabel = QLabel(settings)
        placeLabel.setText(self.tr("Row pane place:"))
        newRowPosition = QComboBox(settings)
        newRowPosition.addItem("Top")
        newRowPosition.addItem("Bottom")
        newRowPosition.addItem("Hide")
        self.connect(newRowPosition, SIGNAL("activated(int)"), self, SLOT("newRowPositionActivated(int)"))
        placeLabel.setBuddy(newRowPosition)
        hl = QHBoxLayout()
        hl.addWidget(placeLabel)
        hl.addWidget(newRowPosition)
        l.addLayout(hl)

        effectLabel = QLabel(settings)
        effectLabel.setText(self.tr("Highlight effect:"))
        newRowEffect = QComboBox(settings)
        newRowEffect.addItem("Flash")
        newRowEffect.addItem("Alpha")
        self.connect(newRowEffect, SIGNAL("activated(int)"), self, SLOT("newRowEffectActivated(int)"))
        effectLabel.setBuddy(newRowEffect)
        hl = QHBoxLayout()
        hl.addWidget(effectLabel)
        hl.addWidget(newRowEffect)
        l.addLayout(hl)

        fastScrollCheck = QCheckBox(settings)
        fastScrollCheck.setText(self.tr("Fast scroll effect"))
        self.connect(fastScrollCheck, SIGNAL("stateChanged(int)"), self, SLOT("fastScrollChanged(int)"))
        l.addWidget(fastScrollCheck)
        fastScrollCheck.setChecked(True)

        dottedLineCheck = QCheckBox(settings)
        dottedLineCheck.setText(self.tr("Dotted grid line"))
        self.connect(dottedLineCheck, SIGNAL("stateChanged(int)"), self, SLOT("dottedLineChanged(int)"))
        l.addWidget(dottedLineCheck)
        dottedLineCheck.setChecked(True)

        label = QLabel(self)
        hl = QHBoxLayout()
        label.setText(self.tr("Grid line style:"))
        lineStylesSelect = QComboBox(settings)
        lineStylesSelect.addItem("None")
        lineStylesSelect.addItem("Both")
        lineStylesSelect.addItem("Both2D")
        lineStylesSelect.addItem("Horizontal")
        lineStylesSelect.addItem("Horizontal2D")
        lineStylesSelect.addItem("Vertical")
        lineStylesSelect.addItem("Vertical2D")
        self.connect(lineStylesSelect, SIGNAL("currentIndexChanged(int)"),
            self, SLOT("selectGridLineStyles(int)"))
        hl.addWidget(label)
        hl.addWidget(lineStylesSelect)
        l.addLayout(hl)
        lineStylesSelect.setCurrentIndex(2)

        zoomEnable = QCheckBox(settings)
        zoomEnable.setText(self.tr("Zoom enabled"))
        zoomEnable.setChecked(True)
        self.connect(zoomEnable, SIGNAL("stateChanged(int)"), self, SLOT("zoomEnabledChanged(int)"))
        l.addWidget(zoomEnable)

        zoomIndicator = QCheckBox(settings)
        zoomIndicator.setText(self.tr("Show zoom indicator"))
        zoomIndicator.setChecked(True)
        self.connect(zoomIndicator, SIGNAL("stateChanged(int)"), self, SLOT("zoomIndicatorChanged(int)"))
        l.addWidget(zoomIndicator)

        zoomSlider = QSlider(settings)
        zoomSlider.setOrientation(Qt.Horizontal)
        zoomSlider.setTickPosition(QSlider.TicksBothSides)
        zoomSlider.setMinimum(25)
        zoomSlider.setMaximum(300)
        zoomSlider.setTickInterval(25)
        zoomSlider.setSingleStep(25)
        zoomSlider.setValue(100)
        self.connect(zoomSlider, SIGNAL("sliderMoved(int)"), self, SLOT("zoomValueChanged(int)"))
        self.connect(self.grid.view(), SIGNAL("zoomChanged(int)"), zoomSlider, SLOT("setValue(int)"))
        l.addWidget(zoomSlider)

        airCheckBox = QCheckBox(settings)
        airCheckBox.setText(self.tr("Windows Air Support"))
        self.connect(airCheckBox, SIGNAL("stateChanged(int)"), self, SLOT("WindowsAirSupportChanged(int)"))
        airCheckBox.setChecked(False)
        l.addWidget(airCheckBox)

        cellAutoRaise = QCheckBox(settings)
        cellAutoRaise.setText(self.tr("Auto raise cell button"))
        self.connect(cellAutoRaise, SIGNAL("stateChanged(int)"), self, SLOT("cellButtonAutoRaiseEnabled(int)"))
        cellAutoRaise.setChecked(True)
        l.addWidget(cellAutoRaise)

        frozenRowsBox = QCheckBox(settings)
        frozenRowsBox.setText(self.tr("Frozen Rows"))
        self.connect(frozenRowsBox, SIGNAL("stateChanged(int)"), self, SLOT("frozenRowsEnabled(int)"))
        frozenRowsBox.setChecked(True)
        l.addWidget(frozenRowsBox)

        transparentBox = QCheckBox(settings)
        transparentBox.setText(self.tr("Transparent Background"))
        self.connect(transparentBox, SIGNAL("stateChanged(int)"), self, SLOT("transparentBackgroundEnabled(int)"))
        transparentBox.setChecked(False)
        l.addWidget(transparentBox)

        printButton = QPushButton(settings)
        printButton.setText(self.tr("Print Preview"))
        self.connect(printButton, SIGNAL("clicked()"), self, SLOT("printPreview()"))
        l.addWidget(printButton)
        return settings

    @QtCore.Slot(int)
    def newRowPositionActivated(self, index):
        view = self.grid.view()
        if index == 0:
            view.options().setNewRowPlace(Qtitan.AtBeginning)
        elif index == 1:
            view.options().setNewRowPlace(Qtitan.AtEnd)
        elif index == 2:
            view.options().setNewRowPlace(Qtitan.AtNone)

    @QtCore.Slot(int)
    def newRowEffectActivated(self, index):
        view = self.grid.view()
        if index == 0:
            view.options().setNewRowHighlightEffect(Qtitan.FlashEffect)
        else:
            view.options().setNewRowHighlightEffect(Qtitan.AlphaEffect)

    @QtCore.Slot(int)
    def fastScrollChanged(self, state):
        view = self.grid.view()
        view.options().setFastScrollEffect(state == Qt.Checked)

    @QtCore.Slot(int)
    def dottedLineChanged(self, state):
        view = self.grid.view()
        pen = view.options().gridLinePen()
        if state == Qt.Checked:
            pen.setStyle(Qt.DotLine)
        else:
            pen.setStyle(Qt.SolidLine)
        view.options().setGridLinePen(pen)

    @QtCore.Slot(int)
    def selectGridLineStyles(self, index):
        view = self.grid.view()
        if index == 0:
            view.options().setGridLines(Qtitan.LinesNone)
        elif index == 1:
            view.options().setGridLines(Qtitan.LinesBoth)
        elif index == 2:
            view.options().setGridLines(Qtitan.LinesBoth2D)
        elif index == 3:
            view.options().setGridLines(Qtitan.LinesHorizontal)
        elif index == 4:
            view.options().setGridLines(Qtitan.LinesHorizontal2D)
        elif index == 5:
            view.options().setGridLines(Qtitan.LinesVertical)
        elif index == 6:
            view.options().setGridLines(Qtitan.LinesVertical2D)
        else:
            view.options().setGridLines(Qtitan.LinesBoth)

    @QtCore.Slot(int)
    def zoomEnabledChanged(self, state):
        view = self.grid.view()
        view.options().setZoomEnabled(state == Qt.Checked)

    @QtCore.Slot(int)
    def zoomIndicatorChanged(self, state):
        view = self.grid.view()
        view.options().setZoomIndicatorActive(state == Qt.Checked)

    @QtCore.Slot(int)
    def zoomValueChanged(self, value):
        factor = (float(value) / 25) * 25
        view = self.grid.view()
        view.options().setZoomFactor(factor / 100)

    @QtCore.Slot(int)
    def WindowsAirSupportChanged(self, state):
        view = self.grid.view()
        view.options().setWindowsAirSupported(state == Qt.Checked)

    @QtCore.Slot(int)
    def cellButtonAutoRaiseEnabled(self, state):
        view = self.grid.view()
        view.options().setCellButtonAutoRaise(state == Qt.Checked)

    @QtCore.Slot(int)
    def frozenRowsEnabled(self, state):
        view = self.grid.view()
        view.options().setRowFrozenButtonVisible(state == Qt.Checked)
        view.options().setFrozenPlaceQuickSelection(state == Qt.Checked)

    @QtCore.Slot(int)
    def transparentBackgroundEnabled(self, state):
        view = self.grid.view()
        view.options().setTransparentBackground(state == Qt.Checked)
        view.options().setAlternatingRowColors(not view.options().alternatingRowColors())

    @QtCore.Slot(ContextMenuEventArgs)
    def contextMenu(self, args):
        args.contextMenu().addAction("Print Preview", self, SLOT("printPreview()"))
        args.contextMenu().addSeparator()
        args.contextMenu().addAction("Developer Machines on the Web", self, SLOT("showCompanyWebSite()"))

    @QtCore.Slot(CellButtonClickEventArgs)
    def cellButtonClicked(self, args):
        QMessageBox.information(self, "Cell button clicked",
            "Clicked: Button - " + str(args.buttonIndex()) + ", Column Title - " + args.column().caption() + ", RowIndex - " + str(args.row().rowIndex()))

    @QtCore.Slot()
    def printPreview(self):
        self.grid.view().printPreview()

    def setShadeColor(self, color):
        self.grid.themeManager().setShadeColor(color)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

.cpp文件:

#include <QApplication>

#include "window.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Window window;
    window.show();
    return app.exec();
}

#include <QtGui>
#include <QDirModel>
#include <QtSql>
#include <QMessageBox>

#include "window.h"

Window::Window()
: DemoMainWindow(QStringLiteral("QtitanDataGrid"), QStringLiteral(QTN_VERSION_DATAGRID_STR), tr("Adding New Row to the grid possibility example"))
{
    Grid::loadTranslation();

    m_grid = new Qtitan::DBGrid(this);

#ifndef Q_OS_ANDROID
    QString path = QApplication::applicationDirPath();
    path += QStringLiteral("/../examples/SQLFiles/assets");
#else
    QString path = QDir::homePath();
    copyAssetFile(path, "database.sqlite");
#endif

    QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), QStringLiteral("database_demo"));
    db.setDatabaseName(path + QStringLiteral("/database.sqlite"));
    db.setHostName(QString());
    db.setPort(-1);
    if (!db.open(QString(), QString()))
    {
        QSqlError err = db.lastError();
        QSqlDatabase::removeDatabase(tr("database_demo"));
        QMessageBox::critical(0, tr("SQL Error"), tr("Error: Can't open database - %1, error - %2").arg(db.databaseName()).arg(err.text()));
        QApplication::exit(1);
        return;
    }

    QSqlTableModel* model = new QSqlTableModel(m_grid, db);
    model->setTable(QStringLiteral("data"));
    model->select();
    if (model->lastError().type() != QSqlError::NoError)
    {
        QMessageBox::critical(0, tr("SQL Error"), tr("Error: SQL data base is not valid."));
        QApplication::exit(1);
        return;
    }
    model->setEditStrategy(QSqlTableModel::OnFieldChange);

    // Configure grid view
    m_grid->setViewType(Qtitan::Grid::BandedTableView);
    Qtitan::GridBandedTableView* view = m_grid->view<Qtitan::GridBandedTableView>();
    view->options().setBandsHeader(false);
    view->options().setColumnAutoWidth(true);
    view->options().setRowAutoHeight(true);
    view->options().setNewRowPlace(Qtitan::AtBeginning);
    view->options().setNewRowHighlightEffect(Qtitan::AlphaEffect);
    view->options().setSupportedDropActions(Qt::CopyAction);
    view->options().setModelItemsDragEnabled(true);

    //Connect Grid's context menu handler.
    connect(view, SIGNAL(contextMenu(ContextMenuEventArgs*)), this, SLOT(contextMenu(ContextMenuEventArgs* )));

    Qtitan::GridTableBand* characteristicsBand = view->addBand(tr("Characteristics"));
    Qtitan::GridTableBand* engineeringBand = view->addBand(tr("Engineering"));
    view->setModel(model);

    Qtitan::GridBandedTableColumn* column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Photo"));
    column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Registration"));
    column->setBandIndex(characteristicsBand->index());
    column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Aircraft"));
    column->setBandIndex(characteristicsBand->index());
    column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Location"));
    column->setBandIndex(characteristicsBand->index());
    column->setRowIndex(1);
    column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Date"));
    column->setBandIndex(characteristicsBand->index());
    column->setRowIndex(2);
    //Add cell button to the column.
    column->addButton(GridColumn::ClearButtonIcon, Qtitan::AtEnd, GridColumn::MouseOverPolicy);
    connect(column, SIGNAL(buttonClicked(CellButtonClickEventArgs*)), this, SLOT(cellButtonClicked(CellButtonClickEventArgs*)));

    column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Photo"));
    column->setEditorType(GridEditor::Picture);
    Qtitan::GridPictureEditorRepository* pictureEditor = (Qtitan::GridPictureEditorRepository *)column->editorRepository();
    Q_UNUSED(pictureEditor);
    column->setBandIndex(engineeringBand->index());
    column->setRowSpan(3);

    column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("History"));
    column->setEditorType(GridEditor::Memo);
    column->setBandIndex(engineeringBand->index());
    column->setRowSpan(3);
    //Add cell button to the column.
    column->addButton(GridColumn::ChoiceButtonIcon, Qtitan::AtEnd);
    connect(column, SIGNAL(buttonClicked(CellButtonClickEventArgs*)), this, SLOT(cellButtonClicked(CellButtonClickEventArgs*)));

    column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Info"));
    column->setEditorType(GridEditor::Memo);
    column->setBandIndex(engineeringBand->index());
    column->setRowSpan(3);
    //Add cell button to the column.
    column->addButton(GridColumn::ChoiceButtonIcon, Qtitan::AtEnd);
    connect(column, SIGNAL(buttonClicked(CellButtonClickEventArgs*)), this, SLOT(cellButtonClicked(CellButtonClickEventArgs*)));

    //Show button menu for all column headers.
    for (int i = 0; i < view->getColumnCount(); ++i)
        static_cast<GridTableColumn *>(view->getColumn(i))->setMenuButtonVisible(true);

    setDemoWidget(m_grid, createSettingsWidget());

    view->bestFit();
}

QWidget* Window::createSettingsWidget()
{
    QWidget* settings = new QWidget(this);
    QVBoxLayout* l = new QVBoxLayout(settings);

    QCheckBox* checkBox = new QCheckBox(settings);
    checkBox->setText(tr("Show Resize Content (new)"));
    connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(showResizeContentChanged(int)));
    checkBox->setChecked(true);
    l->addWidget(checkBox);

    QLabel* placeLabel = new QLabel(settings);
    placeLabel->setText(tr("New Row pane place:"));
    QComboBox* newRowPosition = new QComboBox(settings);
    newRowPosition->addItem(tr("Top"));
    newRowPosition->addItem(tr("Bottom"));
    newRowPosition->addItem(tr("Hide"));
    connect(newRowPosition, SIGNAL(activated(int)), this, SLOT(newRowPositionActivated(int)));
    placeLabel->setBuddy(newRowPosition);
    QHBoxLayout* hl = new QHBoxLayout(0);
    hl->addWidget(placeLabel);
    hl->addWidget(newRowPosition);
    l->addLayout(hl);

    QLabel* effectLabel = new QLabel(settings);
    effectLabel->setText(tr("Highlight effect:"));
    QComboBox* newRowEffect = new QComboBox(settings);
    newRowEffect->addItem(tr("Flash"));
    newRowEffect->addItem(tr("Alpha"));
    connect(newRowEffect, SIGNAL(activated(int)), this, SLOT(newRowEffectActivated(int)));
    effectLabel->setBuddy(newRowEffect);
    hl = new QHBoxLayout(0);
    hl->addWidget(effectLabel);
    hl->addWidget(newRowEffect);
    l->addLayout(hl);

    QCheckBox* fastScrollCheck = new QCheckBox(settings);
    fastScrollCheck->setText(tr("Fast scroll effect"));
    connect(fastScrollCheck, SIGNAL(stateChanged(int)), this, SLOT(fastScrollChanged(int)));
    l->addWidget(fastScrollCheck);
    fastScrollCheck->setChecked(true);

    QCheckBox* dottedLineCheck = new QCheckBox(settings);
    dottedLineCheck->setText(tr("Dotted grid line"));
    connect(dottedLineCheck, SIGNAL(stateChanged(int)), this, SLOT(dottedLineChanged(int)));
    l->addWidget(dottedLineCheck);
    dottedLineCheck->setChecked(true);

    QLabel* label = new QLabel(this);
    hl = new QHBoxLayout(0);
    label->setText(tr("Grid line style:"));
    QComboBox* lineStylesSelect = new QComboBox(settings);
    lineStylesSelect->addItem(tr("None"));
    lineStylesSelect->addItem(tr("Both"));
    lineStylesSelect->addItem(tr("Both2D"));
    lineStylesSelect->addItem(tr("Horizontal"));
    lineStylesSelect->addItem(tr("Horizontal2D"));
    lineStylesSelect->addItem(tr("Vertical"));
    lineStylesSelect->addItem(tr("Vertical2D"));
    connect(lineStylesSelect, SIGNAL(currentIndexChanged(int)), this, SLOT(selectGridLineStyles(int)));
    hl->addWidget(label);
    hl->addWidget(lineStylesSelect);
    l->addLayout(hl);
    lineStylesSelect->setCurrentIndex(2);

    QCheckBox* zoomEnable = new QCheckBox(settings);
    zoomEnable->setText(tr("Zoom enabled"));
    zoomEnable->setChecked(true);
    connect(zoomEnable, SIGNAL(stateChanged(int)), this, SLOT(zoomEnabledChanged(int)));
    l->addWidget(zoomEnable);

    QCheckBox* zoomIndicator = new QCheckBox(settings);
    zoomIndicator->setText(tr("Show zoom indicator"));
    zoomIndicator->setChecked(true);
    connect(zoomIndicator, SIGNAL(stateChanged(int)), this, SLOT(zoomIndicatorChanged(int)));
    l->addWidget(zoomIndicator);

    QSlider* zoomSlider = new QSlider(settings);
    zoomSlider->setOrientation(Qt::Horizontal);
    zoomSlider->setTickPosition(QSlider::TicksBothSides);
    zoomSlider->setMinimum(25);
    zoomSlider->setMaximum(300);
    zoomSlider->setTickInterval(25);
    zoomSlider->setSingleStep(25);
    zoomSlider->setValue(100);
    connect(zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(zoomValueChanged(int)));
    connect(m_grid->view<Qtitan::GridTableView>(), SIGNAL(zoomChanged(int)), zoomSlider, SLOT(setValue(int)));
    l->addWidget(zoomSlider);

    QCheckBox* cellAutoRaise = new QCheckBox(settings);
    cellAutoRaise->setText(tr("Auto raise cell button"));
    connect(cellAutoRaise, SIGNAL(stateChanged(int)), this, SLOT(cellButtonAutoRaiseEnabled(int)));
    cellAutoRaise->setChecked(true);
    l->addWidget(cellAutoRaise);

    QCheckBox* frozenRowsBox = new QCheckBox(settings);
    frozenRowsBox->setText(tr("Frozen Rows"));
    connect(frozenRowsBox, SIGNAL(stateChanged(int)), this, SLOT(frozenRowsEnabled(int)));
    frozenRowsBox->setChecked(true);
    l->addWidget(frozenRowsBox);

    QCheckBox* transparentBox = new QCheckBox(settings);
    transparentBox->setText(tr("Transparent Background"));
    connect(transparentBox, SIGNAL(stateChanged(int)), this, SLOT(transparentBackgroundEnabled(int)));
    transparentBox->setChecked(false);
    l->addWidget(transparentBox);

    QCheckBox* rowSizingBox = new QCheckBox(settings);
    rowSizingBox->setText(tr("Resizing row (new)"));
    connect(rowSizingBox, SIGNAL(stateChanged(int)), this, SLOT(rowSizingEnabled(int)));
    rowSizingBox->setChecked(false);
    l->addWidget(rowSizingBox);

    QPushButton* printButton = new QPushButton(settings);
    printButton->setText(tr("Print Preview"));
    connect(printButton, SIGNAL(clicked()), this, SLOT(printPreview()));
    l->addWidget(printButton);
    return settings;
}

void Window::newRowPositionActivated(int index)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();

    switch (index)
    {
    case 0:
        view->options().setNewRowPlace(Qtitan::AtBeginning);
        break;
    case 1:
        view->options().setNewRowPlace(Qtitan::AtEnd);
        break;
    default:
        view->options().setNewRowPlace(Qtitan::AtNone);
    }
}

void Window::newRowEffectActivated(int index)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();

    switch (index)
    {
    case 0:
        view->options().setNewRowHighlightEffect(Qtitan::FlashEffect);
        break;
    case 1:
        view->options().setNewRowHighlightEffect(Qtitan::AlphaEffect);
        break;
    default:
        view->options().setNewRowHighlightEffect(Qtitan::AlphaEffect);
    }
}

void Window::fastScrollChanged(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setFastScrollEffect(state == Qt::Checked);
}

void Window::dottedLineChanged(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    QPen pen = view->options().gridLinePen();
    pen.setStyle(state == Qt::Checked ? Qt::DotLine : Qt::SolidLine);
    view->options().setGridLinePen(pen);
}

void Window::selectGridLineStyles(int index)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    switch (index)
    {
    case 0:
        view->options().setGridLines(Qtitan::LinesNone);
        break;
    case 1:
        view->options().setGridLines(Qtitan::LinesBoth);
        break;
    case 2:
        view->options().setGridLines(Qtitan::LinesBoth2D);
        break;
    case 3:
        view->options().setGridLines(Qtitan::LinesHorizontal);
        break;
    case 4:
        view->options().setGridLines(Qtitan::LinesHorizontal2D);
        break;
    case 5:
        view->options().setGridLines(Qtitan::LinesVertical);
        break;
    case 6:
        view->options().setGridLines(Qtitan::LinesVertical2D);
        break;
    default:
        view->options().setGridLines(Qtitan::LinesBoth);
    }
 }

void Window::selectViewOrientation(int index)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    switch (index)
    {
    case 0:
        {
            view->options().setViewOrientation(Qt::Vertical);

            Qtitan::GridBandedTableColumn* column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Location"));
            column->setRowIndex(1);
            column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Date"));
            column->setRowIndex(2);
        }
        break;
    case 1:
        {
            view->options().setViewOrientation(Qt::Horizontal);

            Qtitan::GridBandedTableColumn* column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Location"));
            column->setRowIndex(0);
            column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName( tr("Date"));
            column->setRowIndex(0);
        }
        break;
    default:
        break;
    }
}

void Window::zoomEnabledChanged(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setZoomEnabled(state == Qt::Checked);
}

void Window::zoomIndicatorChanged(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setZoomIndicatorActive(state == Qt::Checked);
}

void Window::zoomValueChanged(int value)
{
    double factor = qCeil((double)value / 25) * 25;
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setZoomFactor(factor / 100);
}

void Window::showResizeContentChanged(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setShowResizeContent(state == Qt::Checked);
}

void Window::cellButtonAutoRaiseEnabled(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setCellButtonAutoRaise(state == Qt::Checked);
}

void Window::frozenRowsEnabled(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setRowFrozenButtonVisible(state == Qt::Checked);
    view->options().setFrozenPlaceQuickSelection(state == Qt::Checked);
}

void Window::transparentBackgroundEnabled(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setTransparentBackground(state == Qt::Checked);

    view->options().setAlternatingRowColors(!view->options().alternatingRowColors());
}

void Window::rowSizingEnabled(int state)
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->options().setRowSizingEnabled(state == Qt::Checked);
}

void Window::contextMenu(ContextMenuEventArgs* args)
{
    args->contextMenu()->addAction(tr("Print Preview"), this, SLOT(printPreview()));
    args->contextMenu()->addSeparator();
    args->contextMenu()->addAction(tr("Developer Machines on the Web"), this, SLOT(showCompanyWebSite()));
}

void Window::cellButtonClicked(CellButtonClickEventArgs* args)
{
    QMessageBox::information(this, tr("Cell button clicked"),
        tr("Clicked: Button - %1, Column Title - %2, RowIndex - %3").arg(args->buttonIndex()).arg(args->column()->caption()).arg(args->row().rowIndex()));
}

void Window::printPreview()
{
    m_grid->view<Qtitan::GridTableView>()->printPreview();
}

void Window::setShadeColor(const QColor& color)
{
    m_grid->themeManager()->setShadeColor(color);
}

.h文件:

#ifndef WINDOW_H
#define WINDOW_H

#include <QComboBox>
#include <QLabel>
#include <QCheckBox>
#include <QtitanDBGrid.h>

#include "DemoMainWindow.h"

class Window : public DemoMainWindow
{
    Q_OBJECT
public:
    Window();
    QWidget* createSettingsWidget();
private Q_SLOTS:
    void newRowPositionActivated(int index);
    void newRowEffectActivated(int index);
    void fastScrollChanged(int state);
    void dottedLineChanged(int state);
    void selectGridLineStyles(int index);
    void selectViewOrientation(int index);
    void zoomEnabledChanged(int state);
    void zoomIndicatorChanged(int state);
    void cellButtonAutoRaiseEnabled(int state);
    void frozenRowsEnabled(int state);
    void transparentBackgroundEnabled(int state);
    void rowSizingEnabled(int);
    void contextMenu(ContextMenuEventArgs* args);
    void cellButtonClicked(CellButtonClickEventArgs* args);
    void printPreview();
    void setShadeColor(const QColor& color);
    void zoomValueChanged(int value);
    void showResizeContentChanged(int state);
private:
    Qtitan::DBGrid* m_grid;
};

#endif

点击获取更多文章教程!现在立刻下载Qt免费试用吧!更多Qt类开发工具QtitanRibbonQtitanChartQtitanNavigationQtitanDockingQtitanDataGrid在线订购现直降1000元,欢迎咨询慧都获取更多优惠>>

工具推荐:

  • QtitanRibbon| 下载试用: 遵循Microsoft Ribbon UI Paradigm for Qt技术的Ribbon UI组件,致力于为Windows、Linux和Mac OS X提供功能完整的Ribbon组件。
  • QtitanChart | 下载试用 :是一个C ++库,代表一组控件,这些控件使您可以快速地为应用程序提供漂亮而丰富的图表。并且支持所有主要的桌面操作系统

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn

文章转载自:

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP