Results 1 to 20 of 32

Thread: How to create custom slot in Qt Designer 4.1?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create custom slot in Qt Designer 4.1?

    OK, here are some excepts of my Homestead app that do what I believe you want to do. This code throws up a dialog which contains a list of wholenames selected from a SQL select of type form "LIKE partialname%". Clicking on the proprty_id column and/or the ssn column selects that property or that individual. Clicking the OK or Cancel button closes the dialog.

    Here is the wholenamedlg.h header file:
    Qt Code:
    1. #ifndef WHOLENAMEDLG_H
    2. #define WHOLENAMEDLG_H
    3. .... snip includes ..........
    4.  
    5. #include "ui_wholenamedlg.h"
    6.  
    7.  
    8. class wholenamedlg : public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. wholenamedlg(QWidget *parent = 0, QString partName = "", QString strYear = "2006", QString strSpouseFlag = "F");
    14. QString resultSSN;
    15. QString resultPropID;
    16. QTableView wholeNameView;
    17.  
    18. private:
    19. Ui::wholeNameDlgUI wnui;
    20.  
    21. private slots:
    22. void copyVALUES(const QModelIndex &);
    23.  
    24. };
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 
    "wholeNameDlgUI" is the name given to the dialog during the Designer session.


    Here is the wholenamedlg.cpp file which displays the dialog grid:
    Qt Code:
    1. /*
    2. Program: wholenamedlg.cpp
    3. Description: A dialog class for picking an SSN and/or a name out of
    4. a query created by the SQL LIKE % syntax.
    5. Author: Jerry L Kreps
    6. Date: 11/10/05 - ff
    7. */
    8.  
    9. #include "wholenamedlg.h"
    10. ... snip includes ......
    11.  
    12. wholenamedlg::wholenamedlg(QWidget *parent, QString partName, QString strYear, QString strSpouseFlag)
    13. : QDialog(parent)
    14. {
    15. wnui.setupUi(this);
    16.  
    17. connect(this->wnui.wholeNameView, SIGNAL(clicked(QModelIndex)), this, SLOT(copyVALUES( QModelIndex )));
    18.  
    19. partName.prepend("'");
    20. partName.append("'");
    21. QString queryStr = "SELECT proprty_id,ssn,wholename,sssn,sname,city,address FROM persinfo_";
    22. queryStr.append(strYear);
    23. if (strSpouseFlag == "F") {
    24. queryStr.append(" WHERE wholename LIKE ");
    25. queryStr.append(partName);
    26. queryStr.append(" ORDER BY wholename");
    27. } else {
    28. queryStr.append(" WHERE sname LIKE ");
    29. queryStr.append(partName);
    30. queryStr.append(" ORDER BY sname");
    31. }
    32. QSqlQueryModel *viewModel = new QSqlQueryModel(wnui.wholeNameView);
    33. viewModel->setHeaderData(0, Qt::Horizontal, "ID");
    34. viewModel->setHeaderData(1, Qt::Horizontal, "SSN");
    35. viewModel->setHeaderData(2, Qt::Horizontal, "WholeName");
    36. viewModel->setHeaderData(3, Qt::Horizontal, "SSSN");
    37. viewModel->setHeaderData(4, Qt::Horizontal, "SName");
    38. viewModel->setHeaderData(5, Qt::Horizontal, "City");
    39. viewModel->setHeaderData(6, Qt::Horizontal, "Address");
    40.  
    41. viewModel->setQuery(queryStr);
    42. if (viewModel->lastError().type() == QSqlError::NoError){
    43. wnui.wholeNameView->setModel(viewModel);
    44. if (viewModel->rowCount() > 0){
    45. for (int i = 0; i < viewModel->rowCount(); ++i)
    46. this->wnui.wholeNameView->verticalHeader()->resizeSection(i,20);
    47. for (int i = 0; i < 7; ++i)
    48. wnui.wholeNameView->resizeColumnToContents(i);
    49. }
    50. }
    51. }
    52.  
    53. void wholenamedlg::copyVALUES(const QModelIndex &QMI) {
    54. QVariant value = this->wnui.wholeNameView->model()->data(QMI,0);
    55. if (value.isValid()) {
    56. if (QMI.column() == 0)
    57. this->resultPropID = value.toString();
    58. if (QMI.column() == 1)
    59. this->resultSSN = value.toString();
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 
    "wholeNameView" is the name given in the Designer to the tableview grid containing the data being displayed.

    Here is the main class for the homestead application. The segment shown displays that part of the searchALL() function which calls the wholenamedlg dialog.
    Qt Code:
    1. /*
    2. Program: homestead.cpp
    3. Description: Homestead Application Program (HAP)
    4. Author: Jerry L Kreps
    5. Date: 11/1/2005
    6. */
    7. #include "homestead.h"
    8. #include "wholenamedlg.h"
    9. ...
    10. homestead::homestead(QWidget *parent) : QMainWindow(parent) {
    11. // homestead constructor method
    12. ui.setupUi(this); // draw the gui interface
    13. this->Year65 = 1940; // set birth year for 65 year olds
    14. this->dbYear = "2006";
    15. ...
    16.  
    17. connect(ui.btnSearch, SIGNAL(clicked()), this, SLOT(searchAll()));
    18. ...
    19. ...
    20. } // end of the homestead constructor
    21.  
    22. void homestead::searchAll() {
    23. // user enters value to search for in search text box
    24. // and clicks the radio button indicating its data type
    25. // then clicks the search button
    26. bool foundProp = false;
    27. bool foundPers = false;
    28. QString queryStr = "";
    29. QString seekWN = "'"; // set up for possible wholename search
    30. QString requestString;
    31. requestString = "Searching for: ";
    32. requestString.append(ui.leSearch->text()); // echo search request
    33. if (ui.rbWholeName->isChecked()) {
    34. QString partialName = ui.leSearch->text().trimmed();
    35. if (partialName.contains('%')){
    36. //select proprty_id,ssn,wholename,sssn,sname,city,address from persinfo_2006
    37. //WHERE wholename LIKE 'MILLER/E%' ORDER BY wholename
    38. wholenamedlg dlg(this, partialName, this->dbYear, "F");
    39. if( dlg.exec() == QDialog::Accepted ){
    40. QString strSSN = dlg.resultSSN.trimmed();
    41. QString strPropID = dlg.resultPropID.trimmed();
    42. // was ssn returned from dialog ?
    43. .....
    44. ..... snip lot's of code .....
    45. .....
    To copy to clipboard, switch view to plain text mode 
    Here is what the dialog looks like in action:
    wholenamedlg_shown.jpg
    Last edited by GreyGeek; 13th January 2006 at 15:11.

Similar Threads

  1. Custom signal in qt designer
    By txandi in forum Qt Tools
    Replies: 1
    Last Post: 4th December 2008, 20:25
  2. create a custom slot, hints?
    By pledians in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2008, 14:26
  3. How to create Custom Slot upon widgets
    By ashukla in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2007, 14:07
  4. Replies: 2
    Last Post: 12th July 2007, 09:55
  5. custom slot + Designer
    By bashamehboob in forum Qt Tools
    Replies: 1
    Last Post: 28th April 2006, 15:17

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.