Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Polish
  4. Dodanie kolumny z checkboxem do QTableView
QtWS25 Last Chance

Dodanie kolumny z checkboxem do QTableView

Scheduled Pinned Locked Moved Unsolved Polish
qsqlquerymodelcheckboxqtableview
2 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    drock
    wrote on last edited by
    #1

    Dzień dobry,
    Jak można do daćkolumnęz checkboxem do QTableView opartem na modelu: QSqlQueryModel

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Można użyć modelu proxy, który będzie interpretował jakieś dane (np. "0" i "1") jako checkbox, np.

      class MyProxy : public QIdentityProxyModel
      {
      public:
          MyProxy(QObject* parent) : QIdentityProxyModel(parent) {}
      
          QVariant data(const QModelIndex& index, int role) const override
          {
              if (role == Qt::CheckStateRole && index.column() == 0)
              {
                  QString str_value = QIdentityProxyModel::data(index, Qt::DisplayRole).toString();
                  return (str_value == "1") ? Qt::Checked : Qt::Unchecked;
              }
      
              return QIdentityProxyModel::data(index, role);
          }
      
          bool setData(const QModelIndex& index, const QVariant& value, int role) override
          {
              if (role == Qt::CheckStateRole && index.column() == 0)
              {
                  QString str_value = (value.toInt() == Qt::Checked) ? "1" : "0";
                  return QIdentityProxyModel::setData(index, str_value, Qt::EditRole);
              }
              else
                  return QIdentityProxyModel::setData(index, value, role);
          }
      
          Qt::ItemFlags flags(const QModelIndex& index) const override
          {
              Qt::ItemFlags f = QIdentityProxyModel::flags(index);
              if (index.column() == 0)
                  f |= Qt::ItemIsUserCheckable;
              return f;
          }
      };
      

      oczywiście numer kolumny i dane rozpoznawane jako "zaznaczony" można sobie dostosować.
      Takiego modelu można użyć potem tak:

      QSqlTableModel* model = new QSqlTableModel(parent, database);
      MyProxy* proxy = new MyProxy(parent);
      proxy->setSourceModel(model);
      tableView->setModel(proxy);
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved