Skip to content
  • 143k Topics
    718k Posts
    G
    I have spent a couple of afternoons, can't get it to work; I am just going in circles. Below, I will include both Python and QML sources of a minimal kind-of-working program. There are two features I can't figure out how to implement and I would very much appreciate some assistance. Desired features: Keyboard navigation Editing of cell values As follows: When I click on a cell, I would like it to be highlighted in some way so I know where I am at. After that, I would like to be able to use the tab key or the arrow keys to navigate the table. When I double-click on a cell or press <enter> while highlighted, I would like for that cell to enter edit mode; and, most importantly (which is the part I can't get to work), when I leave edit mode, I would like the new value to be reflected in the table. Here is the Python code: import os import sys import json from pathlib import Path from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine from PySide6.QtCore import ( QAbstractTableModel, QModelIndex, QObject, Qt, Property, Signal, Slot ) script_dir = Path( os.path.dirname(os.path.abspath(__file__)) ) # --- MyTableModel --- # A custom model for use with QML's TableView. # It provides methods for reading, writing, and modifying table data. class MyTableModel(QAbstractTableModel): def __init__(self, header=None, data=None, parent=None): super().__init__(parent) self._header = header or [] self._data = data or [] def rowCount(self, parent=QModelIndex()): return len(self._data) def columnCount(self, parent=QModelIndex()): if self._data: return len(self._data[0]) return 0 def data(self, index, role=Qt.DisplayRole): if not index.isValid(): return None row = index.row() col = index.column() if row >= len(self._data) or col >= len(self._data[0]): return None if role == Qt.DisplayRole: return str(self._data[row][col]) @Slot(int, Qt.Orientation, result="QVariant") def headerData(self, section, orientation, role=Qt.DisplayRole): if role == Qt.DisplayRole: if orientation == Qt.Horizontal: return self._header[section] else: return str(section) # required for editable models def setData(self, index, value, role=Qt.EditRole): if role == Qt.EditRole: if index.isValid() and 0 <= index.row() < self.rowCount() and 0 <= index.column() < self.columnCount(): row = index.row() col = index.column() self._data[row][col] = value self.dataChanged.emit(index, index, [role]) return True return False # required for editable models, to inform the view which roles are editable def flags(self, index): if not index.isValid(): return Qt.NoItemFlags return Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = # --- MyTableManager --- # A QObject that provides the interface between QML and MyTableModel. class MyTableManager(QObject): dataChanged = Signal() def __init__(self, model): super().__init__() self._model = model self._selected_row = -1 @Property(QObject, constant=True) def model(self): return self._model @Slot(str) def loadData(self, filepath): self._model.loadData(filepath) @Slot(int, int, str) def updateValue(self, row, col, value): index = self._model.index(row, col, QModelIndex()) self._model.setData(index, value) if __name__ == "__main__": app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() items = [ [ "1", "Alice" , "30"], [ "2", "Bob" , "25"], [ "3", "Charlie", "35"] ] my_table_model = MyTableModel(data=items) manager = MyTableManager(my_table_model) engine.rootContext().setContextProperty("tableManager", manager) engine.rootContext().setContextProperty("tableModel", my_table_model) qml_file = Path(__file__).parent / "gen.qml" engine.load(qml_file) if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec()) Here is the QML source: import QtQuick import QtQuick.Controls.Basic Window { visible: true width: 320 height: 180 title: qsTr("Hello World") color: '#222222' TableView { id: tableView columnWidthProvider: function (column) { return 100; } rowHeightProvider: function (column) { return 40; } anchors.fill: parent model: tableModel delegate: Rectangle { implicitWidth: 100 implicitHeight: 40 color: (index % 2 === 0) ? "#FFFFFF" : "#F9F9F9" border.width: 1 border.color: "lightgray" Text { text: display anchors.fill: parent anchors.margins: 5 verticalAlignment: Text.AlignVCenter horizontalAlignment: Text.AlignHCenter } TableView.editDelegate: TextField { anchors.fill: parent text: display horizontalAlignment: TextInput.AlignHCenter verticalAlignment: TextInput.AlignVCenter TableView.onCommit: { tableManager.updateValue(row, column, text) } } } } }
  • Jobs, project showcases, announcements - anything that isn't directly development
    4k 23k
    4k Topics
    23k Posts
    Esmaeil_SooanE
    Hi, You can also check out this library: 👉 QAudioTagReader It’s a Qt-friendly wrapper around TagLib for exporting audio tags.
  • Everything related to designing and design tools

    129 391
    129 Topics
    391 Posts
    J
    In QT Design Studio, Tab Buttons are mostly for navigation within the UI, but each tab doesn’t automatically hide other content. Usually, a StackView or Loader is needed to switch visible content depending on the active tab.
  • Everything related to the QA Tools

    81 219
    81 Topics
    219 Posts
    H
    @IamSumit can you reproduce the issue and share the Support Information logs. Please refer to the following Knowledge Base Article regarding how to obtain the Support Information is https://qatools.knowledgebase.qt.io/misc/collecting-support-info/collecting-support-information-linux-mac-windows/
  • Everything related to learning Qt.

    388 2k
    388 Topics
    2k Posts
    L
    Hello! I have recently systematically studied the content related to QML modular development using CMake under Qt 6.8, and tried to write a set of configurations. To ensure that the project structure is clear, complies with modern CMake specifications, and avoids potential hidden dangers in subsequent maintenance, I hope to invite experienced friends to help review my configuration ideas and implementation details to see if there are any non-standard, optimizable, or missing parts. Project structure: D:\PROJECT\QT\MODERNTEST | CMakeLists.txt | \---src CMakeLists.txt Images.qrc main.cpp Test.qml Global CMakeLists.txt: cmake_minimum_required(VERSION 3.16) project(ModernTest VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTORCC ON) find_package(Qt6 6.8 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.8) add_subdirectory(src) Sub CMakeLists.txt: add_executable(${PROJECT_NAME} main.cpp ) qt_add_qml_module(${PROJECT_NAME} URI QMLApp VERSION 0.1 QML_FILES Test.qml RESOURCES Images.qrc ) set_target_properties(${PROJECT_NAME} PROPERTIES MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Quick ) install(TARGETS ${PROJECT_NAME} BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char* argv[]){ QGuiApplication a(argc,argv); QQmlApplicationEngine engine; engine.loadFromModule("QMLTest","Test"); return a.exec(); } The above is the configuration I organized based on what I learned in class and the official manual. To ensure compliance with modern CMake specifications and avoid potential issues, I sincerely ask all experts to kindly offer their advice and check if there are any omissions or "modern" techniques that can be optimized.
  • 2k Topics
    13k Posts
    Kent-DorfmanK
    I do a lot of python/pyqt database access using psycopg2 for PostgreSQL. It's so much easier to prototype these kinds of ideas in a language like python, and if needed then port it to c++, but truth be told, most of the time the python app is just works as is so no need to rewrite in c++. For something as simple as you are alluding to, the hard core OO models using DAO/DTO are probably overkill. Also, when dealing qith SQL queries it's "nice" to work in a language that is NOT strongly typed. Sorry if this is a round-about way to respond. Your path has merit in the realm of intellectual curiosity, but if you want it done quickly then python is a better option, and you can still wrap it in a Qt GUI. Yikes! Didn't notice OP was 9 months ago...
  • 4k Topics
    18k Posts
    L
    我在Qt生成的动态库,是否可以用Visual Studio 中C++直接调用?项目中包含了Qt类型的是否可以生效,比如QString,QTimer? 是否可以用C#调用?
  • This is where all the posts related to the Qt web services go. Including severe sillyness.
    1k 10k
    1k Topics
    10k Posts
    SGaistS
    Hi, I think you can use https://forum.qt.io/category/5/qt-io-webservices category for this type of question (moved there by the way). Are these in your "unread" section ? Are they marked as you following them ?