QTablewidget
-
#include "events_save.h"
#include "ui_events_save.h"events_save::events_save(QWidget *parent) :
QDialog(parent)
{
setWindowTitle("Events Saved");
setFixedSize(400,450);db.setHostName("localhost"); db.setDatabaseName("agenda"); db.setUserName("fadelsew"); db.setPassword("azerty"); if (!db.open()) { qFatal("Erreur de connexion à la base de données : %s", qPrintable(db.lastError().text())); return; } layoutPrincipal = new QGridLayout(this); QLabel *label = new QLabel("Events Saved", this); label->adjustSize(); label->setEnabled(false); label->setFont(QFont("Cursive",15)); combo = new QComboBox(this); combo->addItem("all"); combo->addItem("3"); combo->addItem("5"); combo->addItem("7"); combo->addItem("14"); combo->addItem("31");// combo->setGeometry(10,10,90, 40);
QPushButton *cancelButton = new QPushButton("Cancel",this); layoutPrincipal->addWidget(label,0,0,5,1); layoutPrincipal->addWidget(combo,5,0,1,1); layoutPrincipal->addWidget(cancelButton,5,1,1,5); periodChange(); connect(cancelButton, SIGNAL(clicked()), this, SLOT(close())); connect(combo, SIGNAL(currentTextChanged(QString)), this, SLOT(periodChange()));}
events_save::~events_save()
{}
void events_save::periodChange( )
{
QString item = combo->currentText();
QSqlQuery query;
qDebug() << item;if( item == "all"){ view = new QTableView(this); modal = new QSqlQueryModel; modal->setQuery("SELECT * FROM event"); qDebug() << "Je suis dans ALL"; view->setModel(modal); view->hideColumn(0); view->show(); layoutPrincipal->addWidget(view,0, 1, 5,5); }else{ tableWidget = new QTableWidget(this); QStringList labels = {"Nom", "Description", "Couleur", "Date", "Heure", "Musique"}; tableWidget->setHorizontalHeaderLabels(labels); tableWidget->setColumnCount(6); int days = item.toInt(); QDate startDate = QDate::currentDate(); QDate endDate = startDate.addDays(days); query.prepare("SELECT nom, description, color, date, heure, music FROM event WHERE date BETWEEN :startDate AND :endDate"); query.bindValue(":startDate", startDate.toString("yyyy-MM-dd")); query.bindValue(":endDate", endDate.toString("yyyy-MM-dd")); if (!query.exec()) { qFatal("Erreur d'exécution de la requête : %s", qPrintable(query.lastError().text())); return; } int row = 0; while (query.next()) { QString nom = query.value(0).toString(); QString description = query.value(1).toString(); QString couleur = query.value(2).toString(); QString date = query.value(3).toString(); QString heure = query.value(4).toString(); QString musique = query.value(5).toString(); tableWidget->setItem(row, 0, new QTableWidgetItem(nom)); tableWidget->setItem(row, 1, new QTableWidgetItem(description)); tableWidget->setItem(row, 2, new QTableWidgetItem(couleur)); tableWidget->setItem(row, 3, new QTableWidgetItem(date)); tableWidget->setItem(row, 4, new QTableWidgetItem(heure)); tableWidget->setItem(row, 5, new QTableWidgetItem(musique)); qDebug() << nom << description << couleur << date << heure << musique; row++; } for(int i=0; i<tableWidget->columnCount(); i++){ tableWidget->resizeColumnToContents(i); } layoutPrincipal->addWidget(tableWidget,0, 1, 5,5); }}
I would like to get the expected events in the user-defined temporary range. I do not get the expected display when I change the display period. But it doesn't work. Help me please!!!
-
S SGaist moved this topic from French on
-
Hi,
What exactly is happening ?
What are you expecting ?
Which version of Qt are you using ?
On which platform ?On a side note, please use coding tags around your code to make it readable.
-
Hi,
What exactly is happening ?
What are you expecting ?
Which version of Qt are you using ?
On which platform ?On a side note, please use coding tags around your code to make it readable.
@SGaist
I work on linux, with version 6.3 of Qt.
I would like that when the user selects a period in the comboBox, it shows him either in a qtableview if he selects the option "all" or in a qtablewidget if he makes another choice, the content of the executed query. But the problem is that when an option other than "all" is selected, I get no return in my qtablewidget.Sorry, I am still a beginner in qt programming. And I don't understand what you put in "coding tags around your code". Thanks for understanding
-
@SGaist
I work on linux, with version 6.3 of Qt.
I would like that when the user selects a period in the comboBox, it shows him either in a qtableview if he selects the option "all" or in a qtablewidget if he makes another choice, the content of the executed query. But the problem is that when an option other than "all" is selected, I get no return in my qtablewidget.Sorry, I am still a beginner in qt programming. And I don't understand what you put in "coding tags around your code". Thanks for understanding
@fadelsew said in QTablewidget:
And I don't understand what you put in "coding tags around your code".
When typing in a block of code, or if you re-edit your first post:
- Select the complete block of code (no more, no less).
- In the "toolbar" here just above where you type your post there is an "icon" which looks like
</>and has a tooltip which shows the word Code. Click it.
You see that it has put a line above your code block and another below it, both of which read
```(3 backtick characters). Post it like that. Your code block is now nicely formatted so we can read it. Thanks.But the problem is that when an option other than "all" is selected, I get no return in my qtablewidget.
"No return", what does that mean? Empty?? You do not tell us whether your
qDebug() << nom << description << couleur << date << heure << musique;is hit, we cannot guess?
So far as I can see you create new table view or table widget in
periodChange()but I don't see that you "get rid of" these for the next time. So you may have multiple widgets left over.I'm afraid to say that this whole approach of constantly creating new models/tables/widgets is a bad idea. Basically you should only need to create your widgets/layouts once at the beginning and then use them as required. If you want quite different "views" for different situations, consider using QStackedLayout to swap between them.
-
@fadelsew said in QTablewidget:
And I don't understand what you put in "coding tags around your code".
When typing in a block of code, or if you re-edit your first post:
- Select the complete block of code (no more, no less).
- In the "toolbar" here just above where you type your post there is an "icon" which looks like
</>and has a tooltip which shows the word Code. Click it.
You see that it has put a line above your code block and another below it, both of which read
```(3 backtick characters). Post it like that. Your code block is now nicely formatted so we can read it. Thanks.But the problem is that when an option other than "all" is selected, I get no return in my qtablewidget.
"No return", what does that mean? Empty?? You do not tell us whether your
qDebug() << nom << description << couleur << date << heure << musique;is hit, we cannot guess?
So far as I can see you create new table view or table widget in
periodChange()but I don't see that you "get rid of" these for the next time. So you may have multiple widgets left over.I'm afraid to say that this whole approach of constantly creating new models/tables/widgets is a bad idea. Basically you should only need to create your widgets/layouts once at the beginning and then use them as required. If you want quite different "views" for different situations, consider using QStackedLayout to swap between them.
-
@fadelsew said in QTablewidget:
And I don't understand what you put in "coding tags around your code".
When typing in a block of code, or if you re-edit your first post:
- Select the complete block of code (no more, no less).
- In the "toolbar" here just above where you type your post there is an "icon" which looks like
</>and has a tooltip which shows the word Code. Click it.
You see that it has put a line above your code block and another below it, both of which read
```(3 backtick characters). Post it like that. Your code block is now nicely formatted so we can read it. Thanks.But the problem is that when an option other than "all" is selected, I get no return in my qtablewidget.
"No return", what does that mean? Empty?? You do not tell us whether your
qDebug() << nom << description << couleur << date << heure << musique;is hit, we cannot guess?
So far as I can see you create new table view or table widget in
periodChange()but I don't see that you "get rid of" these for the next time. So you may have multiple widgets left over.I'm afraid to say that this whole approach of constantly creating new models/tables/widgets is a bad idea. Basically you should only need to create your widgets/layouts once at the beginning and then use them as required. If you want quite different "views" for different situations, consider using QStackedLayout to swap between them.
-
@JonB Concerning the return I was talking about above, I was talking about the return of my request to my database which should return the elements contained in the time range chosen by the user