Results 1 to 7 of 7

Thread: QPushButton,Several unclicked clicks too many

  1. #1
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QPushButton,Several unclicked clicks too many

    I have made an application that inserts some data into an sqlite database.The problem is once i click save it inserts several duplicate records instead of just one.What could be the problem?.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QPushButton,Several unclicked clicks too many

    There could be a so-called bug in the application...

    First thing to check is to find out how many clicks the button really emits. There might be only one and the flaw could be somewhere else, having nothing to do with the pushbutton.

    But even if the button misbehaves, your application has a problem if it inserts duplicate records. (This is the traditional problem with badly designed web applicatios where the impatient user clicks the button several times thinking that the click was not received, ending with the form data sent several times, and saved several times.)

  3. #3
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default Re: QPushButton,Several unclicked clicks too many

    I might be way off, but may i suggest disabling your pushbutton once it has been clicked to make sure that only one click occurs. use command:
    ui->pushButton->setEnabled(false) to disable it in the pushButton command.

  4. #4
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPushButton,Several unclicked clicks too many

    Here is the code,i couldn't post yesterday since i was away from my computer.

    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include "smith.h"
    4.  
    5. smith::smith(QWidget *parent) :
    6. QWidget(parent)
    7. {
    8. ui.setupUi(this);
    9. setupModel();
    10.  
    11. //tables Start
    12. tablesModel = new QSqlTableModel(ui.tables);
    13. tablesModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    14. tablesModel->setTable("tables");
    15.  
    16. tablesModel->select();
    17.  
    18. ui.tables->setModel(tablesModel);
    19.  
    20. ui.tables->setColumnHidden(tablesModel->fieldIndex("id"), true);
    21.  
    22. ui.tables->setEditTriggers(QAbstractItemView::NoEditTriggers);
    23. ui.tables->setSelectionMode(QAbstractItemView::SingleSelection);
    24.  
    25. ui.tables->setSortingEnabled(true);
    26.  
    27. ui.tables->setSelectionBehavior(QAbstractItemView::SelectRows);
    28. ui.tables->horizontalHeader()->setStretchLastSection(true);
    29.  
    30. //localized header captions
    31. tablesModel->setHeaderData(1, Qt::Horizontal, tr("Table Name"));
    32. tablesModel->setHeaderData(2, Qt::Horizontal, tr("Table Description"));
    33.  
    34. connect(ui.tables->selectionModel(),
    35. SIGNAL(currentRowChanged(const QModelIndex &,
    36. const QModelIndex &)),
    37. this, SLOT(updateView()));
    38.  
    39. //tables end
    40. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. {
    2. ...
    3. //UPDATE VIEW FUNCTION
    4. tableModel->select();
    5. //qt center question starts here
    6. ui.tableView->setModel(tableModel);
    7. ui.tableView->setColumnHidden(tableModel->fieldIndex("id"), true);
    8.  
    9. ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    10. ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    11.  
    12. ui.tableView->setSortingEnabled(true);
    13.  
    14. ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    15. ui.tableView->horizontalHeader()->setStretchLastSection(true);
    16. //localized header captions
    17. tableModel->setHeaderData(1, Qt::Horizontal, tr("First Name"));
    18. tableModel->setHeaderData(2, Qt::Horizontal, tr("Last Name"));
    19. tableModel->setHeaderData(3, Qt::Horizontal, tr("Country"));
    20. tableModel->setHeaderData(4, Qt::Horizontal, tr("City"));
    21.  
    22. //Mapper
    23. QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
    24. mapper->setModel(tableModel);
    25. mapper->addMapping(ui.fEdit, 1);
    26. mapper->addMapping(ui.lnEdit, 2);
    27. mapper->addMapping(ui.cEdit, 3);
    28. mapper->addMapping(ui.cityEdit, 4);
    29. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    30. mapper->toFirst();
    31.  
    32.  
    33. connect(ui.tableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    34. mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    35.  
    36. connect(ui.newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked()));
    37. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(on_cancelButton_clicked()));
    38. connect(ui.nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
    39. connect(ui.previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
    40. connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
    41. connect(ui.deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked()));
    42. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QPushButton,Several unclicked clicks too many

    For each connect statement you will get exactly one slot call on signal emit (even if you click one time, if connect was called twice, the slot will be called twice ), so verify that you didn't connect the button more than once (for example, once by connectSlotsByName and once manually).

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushButton,Several unclicked clicks too many

    pass Qt::UniqueConnection to the connect statement to avoid duplicates.

  7. The following user says thank you to nish for this useful post:

    thefatladysingsopera (16th August 2011)

  8. #7
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPushButton,Several unclicked clicks too many

    Thanks all.I have used
    Qt Code:
    1. connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()), Qt::UniqueConnection);
    To copy to clipboard, switch view to plain text mode 
    and works well.

Similar Threads

  1. Simulate left and right clicks?
    By hakermania in forum Newbie
    Replies: 5
    Last Post: 8th June 2011, 12:44
  2. Replies: 2
    Last Post: 20th December 2010, 18:51
  3. Replies: 3
    Last Post: 12th May 2010, 14:11
  4. Generating Mouse Clicks
    By Ebonair in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2009, 19:20
  5. Replies: 3
    Last Post: 26th September 2006, 13:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.