int main(int argc,char* argv[]) { QApplication app(argc,argv); QTableWidget* table = new QTableWidget(); table->setWindowTitle("Connect to Mysql Database Example"); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("192.168.11.3"); db.setDatabaseName("menudb"); db.setUserName("root"); db.setPassword("test"); if (!db.open()) { QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text()); } QSqlQuery query("SELECT * FROM test"); table->setColumnCount(query.record().count()); table->setRowCount(query.size()); int index=0; while (query.next()) { table->setItem(index,0,new QTableWidgetItem(query.value(0).toString())); table->setItem(index,1,new QTableWidgetItem(query.value(1).toString())); index++; } table->show(); return app.exec(); }
int main(int argc, char *argv[]) { QApplication a(argc, argv); const int n = 3; QTableWidget *mainTable = new QTableWidget(n,n); QTableWidgetItem *widgetItem = 0; QStringList nameItems; nameItems << "First" << "Second" << "Third"; mainTable->setHorizontalHeaderLabels(nameItems); //set headers on Horizontal mainTable->setVerticalHeaderLabels(nameItems); //set headers on Vertical for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { widgetItem = new QTableWidgetItem(QString("%1,%2").arg(i).arg(j)); mainTable->setItem(i,j,widgetItem); } } mainTable->setGeometry(430,340,400,180); mainTable->setWindowTitle("Main table"); mainTable->show(); return a.exec(); }
void MainWindow::showTable() { QTextCursor cursor = editor->textCursor(); QTextTable *table = cursor.currentTable(); if (!table) return; QTableWidget *tableWidget = new QTableWidget(table->rows(), table->columns()); //! [9] for (int row = 0; row < table->rows(); ++row) { for (int column = 0; column < table->columns(); ++column) { QTextTableCell tableCell = table->cellAt(row, column); //! [9] QTextFrame::iterator it; QString text; for (it = tableCell.begin(); !(it.atEnd()); ++it) { QTextBlock childBlock = it.currentBlock(); if (childBlock.isValid()) text += childBlock.text(); } QTableWidgetItem *newItem = new QTableWidgetItem(text); tableWidget->setItem(row, column, newItem); /* //! [10] processTableCell(tableCell); //! [10] */ //! [11] } //! [11] //! [12] } //! [12] tableWidget->setWindowTitle(tr("Table Contents")); tableWidget->show(); }