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. MySQL database table row value display in QTableView like below mentioned image
QtWS25 Last Chance

MySQL database table row value display in QTableView like below mentioned image

Scheduled Pinned Locked Moved General and Desktop
19 Posts 3 Posters 9.6k 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.
  • H Offline
    H Offline
    highlander
    wrote on last edited by
    #1

    !http://i.stack.imgur.com/2etav.png(Image)!

    I need to show the primary key column values like specified in the above image. Can it be achieved? As QTableView will only make rows and columns available in the DB table, I need only particular values (of primary key column) iterated with for(each) loop inside while(query.next())...kindly guide me, thank you in advance.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Similar issue was discussed yesterday. Look at "link..":http://qt-project.org/forums/viewthread/24737/#114214

      1 Reply Last reply
      0
      • H Offline
        H Offline
        highlander
        wrote on last edited by
        #3

        Thanks qxoz, in that conversation, Andre said "just use a table configured to use single row selection. You can always add the visual effect of a radio button later", does he meant to use QTableView to display one row of the values (primary key column) and add radio button besides them?? so the radio buttons will look like in another column? is it possible?

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          No he meant radiobuttons are last thing to do. Radiobutton is only a visual effect, selecting single row of TableView can be obtained by setting single sellection flag for QTableView. But if you want exactly radiobuttons then solution from Andre
          [quote author="Andre" date="1360674416"]If you really want, you can however visually represent the selection with a radio button using a proxy model. The proxy model could take the QItemSelectionModel. Depending on if the row is selected, it could give a different result for the Qt::DecorationRole of the first column. Just use two images, one of a checked and one of an unchecked radio button. For bonus points, use the current style to render them :-)[/quote]
          can be a good choice. In this case radiobutton will be displayed as icon and you can place it in one column with data or in different columns as you wish.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Starts to sound like a school assignment to me...

            1 Reply Last reply
            0
            • H Offline
              H Offline
              highlander
              wrote on last edited by
              #6

              bawahaha... andre, well i am a newly joined kid in School of Qt :), I need real help to get this assignment done :( ...

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                I suggest you go to your teacher for help then.

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  highlander
                  wrote on last edited by
                  #8

                  He himself told me to post it here :-D

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    He's paid to help you, we are not.

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      highlander
                      wrote on last edited by
                      #10

                      Why so serious dear Andre.... thanks anyway

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        Because you're not showing your own effort. The point of your excersise is for you to learn Qt. Just giving you ready-made solutions won't teach you anything at all.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qxoz
                          wrote on last edited by
                          #12

                          That's great that the interest to Qt grows and grows. And all will be happy to help (at least i am) when possible. Just newcomers need to be more attentive and keep the forum rules. In the first place to search for similar topics on the forum and then create new topics. And try harder to solve issues their own.

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            highlander
                            wrote on last edited by
                            #13

                            OK From a little research I got this url: "It shows the ListModel, I guess thats what I need right?":http://files.itslearning.com/data/764/2405/qt4/ch10lev1sec3.html. Can anyone provide examples for this ListModel?

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #14

                              Why don't you take this step by step? Your first step would be to get the data from the database into something that you can show in a QListView. Qt happens to have a few suitable models for that already... They both have 'Sql' in their name.

                              I stand by my advice to ignore the radio button for the time being.

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                highlander
                                wrote on last edited by
                                #15

                                Hi Andre, I have completed the First step as you said, here is my code:

                                @#include "mainwindow.h"
                                #include "ui_mainwindow.h"
                                #include <QStandardItemModel>
                                #include <QListView>
                                #include <QSqlDatabase>
                                #include <QSqlQueryModel>
                                #include <QListView>

                                MainWindow::MainWindow(QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::MainWindow)
                                {
                                ui->setupUi(this);
                                QListView *listView = new QListView(this);
                                QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
                                db.setHostName("localhost");
                                db.setDatabaseName("users_in_feb");
                                db.setUserName("web_me");
                                db.setPassword("");
                                if(!db.open())
                                {
                                qDebug()<< "Failed";
                                qDebug()<< db.lastError();
                                }
                                else
                                {
                                model = new QSqlQueryModel(this);
                                model->setQuery("Select Name from new_joiners");
                                listView->setModel(model);
                                listView->setFixedSize(300,300); //width and height of QListView
                                }
                                }

                                MainWindow::~MainWindow()
                                {
                                db.close();
                                delete ui;
                                }

                                void MainWindow::changeEvent(QEvent *e)
                                {
                                QMainWindow::changeEvent(e);
                                switch (e->type()) {
                                case QEvent::LanguageChange:
                                ui->retranslateUi(this);
                                break;
                                default:
                                break;
                                }
                                }
                                @

                                And the output : !http://i46.tinypic.com/2ykywjm.jpg!

                                Now it seems dont need radio buttons, but I need to send selected Name to another window. Guide me thanks.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #16

                                  Well, time to dive into using signals and slots then... QListView exposes a couple of interesting signals you might use.

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    highlander
                                    wrote on last edited by
                                    #17

                                    Andre, I am not able to get what I need in that Signals, in my mind, my need is like: "When Next > button is clicked, it must check if a value is selected in QListView or left empty, if empty, show an alert MessageBox, else the selected value must be submitted to the next Window and shown in the QLineEdit field." Help me, thanks.

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andre
                                      wrote on last edited by
                                      #18

                                      No, that is not what you need. It is bad usability. Instead of letting the user click the Next button, the next button should be disabled as long as there isn't anything selected yet. If you want to react on a click on the button, then obviously you need to take a look at the signals provided by that button, not by the list view.

                                      Note that it seems that you are trying to make a wizard. Why not use QWizard for that?

                                      1 Reply Last reply
                                      0
                                      • H Offline
                                        H Offline
                                        highlander
                                        wrote on last edited by
                                        #19

                                        Hi Andre, ur right, but in QListView, automatically the first element is selected when app starts, what can I do?

                                        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