Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Model/View QTable displays nothing - just a white field :(

[SOLVED] Model/View QTable displays nothing - just a white field :(

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.6k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    MisteKiste
    wrote on last edited by
    #1

    Hey Guys,

    I have some problems with the Model/View Architecture from QT. I want to display some data into a QTableView from my own Model.

    generalmodel.h
    @namespace Ui {
    class MainWindow;
    }

    class GeneralModel: public QAbstractTableModel
    {
    Q_OBJECT
    public:
    GeneralModel(QWidget *parent);
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

    void test();
    

    private:
    Ui::MainWindow *mainWindow;
    };
    @

    generalmodel.cpp
    @GeneralModel::GeneralModel(QWidget *parent)
    :QAbstractTableModel(parent)

    {

    }

    int GeneralModel::rowCount(const QModelIndex & /parent/) const
    {
    return 2;
    }

    int GeneralModel::columnCount(const QModelIndex & /parent/) const
    {
    return 3;
    }

    QVariant GeneralModel::data(const QModelIndex &index, int role) const
    {
    if (role == Qt::DisplayRole)
    {
    return QString("Row%1, Column%2")
    .arg(index.row() + 1)
    .arg(index.column() +1);
    }
    return QVariant();
    }

    void GeneralModel::test()
    {
    qDebug() << "DATAMODEL!";
    }@

    I declare the model and table in a mainwindow class in the constructor.

    mainwindow.h
    @
    class GeneralModel;

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_generalSafeButton_clicked();

    void on_action_ffnen_triggered();
    

    private:
    Ui::MainWindow *ui;
    Api *api;
    QTableView *qTable;
    GeneralModel *gModel;

    };@

    mainwindow.cpp
    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    api(new Api)
    {

    ui->setupUi(this);
    
    
    GeneralModel gm(0);
    static QTableView *qTable = new QTableView();
    qTable->setModel(&gm);
    qTable->show();
    

    }@

    Well, when I compile my program I only get an empty table. But I don't understand why. The funny thing is, when I paste the exactly same code in my main class it works. Like this.

    main class
    @int main(int argc, char *argv[])
    {

    QApplication a(argc, argv);
    
    GeneralModel gm(0);
    QTableView *qTable = new QTableView();
    qTable->setModel(&gm);
    qTable->show();
    
    MainWindow w;
    w.show();
    return a.exec(&#41;;
    

    }@

    Please help me. I am looking for hours to fix this problem.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      In mainwindow.cpp, as soon as your constructor ends gm goes out of scope and is destroyed hence you don't see anything because the model is gone even before the table view is shown.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MisteKiste
        wrote on last edited by
        #3

        Okay and how could my gm Model lives out of scope?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Create it on the heap.

          Also be aware that with your current code you are likely to get into trouble.

          You should take a look at Qt's documentation model/view examples before going further.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MisteKiste
            wrote on last edited by
            #5

            Yeah thank you :). It's working now. The current code was only an example to show you my problem earlier.

            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