Forum
Sign Up
× DataGrid for Qt.C++

How to add columns to a grid

9 years 7 months ago #1 by Aaron
        // "this" is a QWidget
	auto layout = new QHBoxLayout(this);

	auto grid = new Qtitan::Grid(this);
	grid->setViewType(Qtitan::Grid::TableView);

	auto view = grid->view<Qtitan::GridTableView>();
	view->options().setColumnAutoWidth(true);
	view->options().setRowAutoHeight(true);
	view->options().setNewRowPlace(Qtitan::NewRowTop);
	view->options().setGridLineWidth(1);

	// Do I need to set a model?
	view->setModel(new QStandardItemModel);

        // This doesn't appear to add a column and is returning a nullptr
        auto column = view->addColumn("test", "test");


	layout->addWidget(grid);
	layout->setContentsMargins(0, 0, 0, 0);

I'm attempting to learn how to use QTitanDataGrid but I am unable to figure out how to simply add columns or rows. None of the demos appear to show how either.

I want to make a simple table with some simple columns/rows.

Any suggestions on what is required?

Please Log in or Create an account to join the conversation.

More
9 years 7 months ago - 9 years 7 months ago #2 by corrado valeri
Replied by corrado valeri on topic How to add columns to a grid
QtitanGRid is build in native QT, that means that you have to first understand how Qt is thought. The grid is an implentation of the view tier of the well known Model View Controller (aka MVC) paradigm. Have a look at qt-project.org/doc/qt-4.8/model-view-programming.html .

If you want to add a column, the best practice to do so it's to add a column to the model that the grid is showing.
Every demo included in qtitangrid has the same concept: build a model, populate it, build a grid and attach it as a view of the model.

For example (taken from drang and drop demo):
m_grid1 = new Qtitan::Grid();
    m_grid1->setViewType(Qtitan::Grid::TableView);
    Qtitan::GridTableView* view1 = m_grid1->view<Qtitan::GridTableView>();

   // the model! add your columns here!!!

    QStandardItemModel* model1 =  new QStandardItemModel(4, 4);
    for (int row = 0; row < 4; ++row)
    {
        for (int column = 0; column < 4; ++column)
        {
            QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
            model1->setItem(row, column, item);
        }
    }

     view1->setModel(model1);
Last edit: 9 years 7 months ago by corrado valeri.

Please Log in or Create an account to join the conversation.

  • corrado valeri
  • corrado valeri's Avatar
9 years 7 months ago - 9 years 7 months ago #3 by Aaron
Replied by Aaron on topic How to add columns to a grid
Thank you for the help so far. That indeed works and generates a 4x4 table.

My next problem is the follow:
auto grid = new Grid();
grid->setViewType(Grid::TableView);
auto view = grid->view<GridTableView>();
auto model = new QStandardItemModel();
view->setModel(model);

QStringList titles;

titles.push_back("Col1");
titles.push_back("Col2");
titles.push_back("Col3");
// etc...

model->setColumnCount(titles.size());
model->setHorizontalHeaderLabels(titles);

The above code yields a table of 0 columns with no Header Labels. I'm attempting a dynamic table which loads X columns from a config file. I need to be able to add any number of columns from a given config file.

Any ideas why this results in 0 columns?

Edit:
model->setColumnCount(10);
view->setModel(model);

It appears I have to set the column count before I set the model???
Last edit: 9 years 7 months ago by Aaron.

Please Log in or Create an account to join the conversation.

More
  • Not Allowed: to create new topic.
  • Not Allowed: to reply.
  • Not Allowed: to edit your message.
Moderators: Developer Machines
Time to create page: 0.127 seconds

Developer Newsletter

Join our Developer Machines newsletter to get informed on all the latest releases of the commercial components for Qt.C++, Delphi FireMonkey, updates and general knowledges.

Quick Support

Should you need any additional information about our products or licensing, please contact us at the following email addresses:

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

Get in Touch

If you would like to purchase our products or services, but don’t know how to do it the right way, please feel free to contact us:

  • This email address is being protected from spambots. You need JavaScript enabled to view it.( any questions related to our products or services )
  • This email address is being protected from spambots. You need JavaScript enabled to view it.( questions related to licensing )