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

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值