Results 1 to 12 of 12

Thread: Dynamically created buttons to one slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    2

    Default Re: Dynamically created buttons to one slot

    ok,

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPushButton>
    4. #include <QFont>
    5. #include <QSizePolicy>
    6. #include <QSignalMapper>
    7. #include <QGroupBox>
    8. #include <QFileDialog>
    9. #include <QtCore/qmath.h>
    10.  
    11. DCB dcb;
    12. COMMTIMEOUTS CommTimeouts;
    13. QPushButton * btn[16];
    14.  
    15.  
    16. MainWindow::MainWindow(QWidget *parent, struct ProfileParams profPar) :
    17. QMainWindow(parent),
    18. ui(new Ui::MainWindow)
    19. {
    20. memset(&dcb,0,sizeof(dcb));
    21. dcb.DCBlength = sizeof(dcb);
    22. dcb.BaudRate = CBR_115200;
    23. dcb.ByteSize = 8;
    24. dcb.Parity = NOPARITY;
    25. dcb.StopBits = ONESTOPBIT;
    26.  
    27. memset(&CommTimeouts, 0x00, sizeof(CommTimeouts));
    28. CommTimeouts.ReadIntervalTimeout = 20;
    29. CommTimeouts.ReadTotalTimeoutConstant = 100;
    30. CommTimeouts.ReadTotalTimeoutMultiplier = 10;
    31. CommTimeouts.WriteTotalTimeoutConstant = 100;
    32. CommTimeouts.WriteTotalTimeoutMultiplier = 10;
    33. std::string slash = "\\\\.\\COM";
    34. std::stringstream sstm;
    35. sstm << slash << profPar.com;
    36. std::string portNumber = sstm.str();
    37. char* aaa;
    38. aaa = new char (portNumber.size());
    39. ComParameters ComPar = {aaa};
    40. char* str;
    41. str = new char(portNumber.size() + 1);
    42. strcpy(str, portNumber.c_str());
    43. ComPar.name = str;
    44. ComPar.dcb = dcb;
    45. ComPar.CommTimeouts = CommTimeouts;
    46.  
    47.  
    48. if(comHandle = initCom(ComPar))
    49. {
    50. ui->setupUi(this);
    51. this->showMaximized();
    52. ui->statusBar->showMessage("Port COM"+QString::number(profPar.com)+" opened", 3000);
    53. ui->networkBox->setTitle(QString::fromStdString(profPar.name));
    54.  
    55. QList<QLayout*> m_layouts;
    56. QSignalMapper* sendSignalMapper = new QSignalMapper(this);//sendMessage
    57. int k = 0, l = 1, a = 0;
    58.  
    59.  
    60.  
    61. for(int b=0; b<profPar.sectors; b++)
    62. {
    63. QGroupBox *groupBox = new QGroupBox(QString::fromStdString(profPar.sectorTab[b][1]));
    64. groupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    65. QVBoxLayout *vbox = new QVBoxLayout;
    66. groupBox->setLayout(vbox);
    67. ui->tlayout->addWidget(groupBox);
    68. m_layouts.append(vbox);
    69. }
    70.  
    71. for(int j=0; j<profPar.number; j++)
    72. {
    73. btn[j] = new QPushButton;
    74. int x = QString::fromStdString(profPar.nameTab[j][2]).toInt() - 1;
    75. btn[j]->setObjectName(QString::fromStdString(profPar.nameTab[j][1]));
    76. btn[j]->setText(QString::fromStdString(profPar.nameTab[j][1]));
    77. btn[j]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    78. btn[j]->setFont(QFont("MS Shell Dlq 2", 14, 1));
    79. sendSignalMapper->setMapping(btn[j],j);
    80. connect(btn[j],SIGNAL(clicked()),sendSignalMapper,SLOT(map()));
    81. m_layouts[x]->addWidget(btn[j]);
    82. }
    83. connect(sendSignalMapper,SIGNAL(mapped(int)),this ,SLOT(send_data(int)));
    84. connect(ui->startButton, SIGNAL(clicked()), this, SLOT(startLstn()));
    85. connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stopLstn()));
    86. connect(ui->checkButton, SIGNAL(clicked()), this, SLOT(close()));
    87. connect(ui->logButton, SIGNAL(clicked()), this, SLOT(openLog()));
    88.  
    89. }
    90. else
    91. {
    92. QMessageBox::critical(this,"Error", "Cannot open", QMessageBox::Ok);
    93. exit(0);
    94. }
    95. }
    96.  
    97. MainWindow::~MainWindow()
    98. {
    99. delete ui;
    100. }
    101.  
    102. void MainWindow::send_data(int number)
    103. {
    104. sendDat("#WAITER_OFF", 1);
    105. }
    106.  
    107. void MainWindow::startLstn()
    108. {
    109. if(startListen())
    110. {
    111. ui->statusBar->showMessage("Listening...");
    112. ui->checkButton->setEnabled(false);
    113. ui->startButton->setEnabled(false);
    114. ui->stopButton->setEnabled(true);
    115. ui->atButton->setEnabled(false);
    116. ui->logButton->setEnabled(false);
    117. }
    118. }
    119.  
    120.  
    121. void MainWindow::stopLstn()
    122. {
    123. if(stopListen())
    124. {
    125. ui->statusBar->showMessage("Stopped", 2000);
    126. ui->checkButton->setEnabled(true);
    127. ui->startButton->setEnabled(true);
    128. ui->stopButton->setEnabled(false);
    129. ui->atButton->setEnabled(true);
    130. ui->logButton->setEnabled(true);
    131. }
    132. }
    133.  
    134. void MainWindow::openLog()
    135. {
    136. QString file = QFileDialog::getOpenFileName(this,tr("Load log file..."),"",tr("Log Files (*.bin)"));
    137. if(!file.isEmpty())
    138. {
    139. logfile *lgfile = new logfile(this,file);
    140. lgfile ->show();
    141. }
    142. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamically created buttons to one slot

    Apart from global array of buttons the code looks correct.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    rdkk (4th June 2013)

  4. #3
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Dynamically created buttons to one slot

    Your code seems good but try to create a separate QPushButton Object and then add it to your array.
    Look below code
    Qt Code:
    1. QPushButton* button = new QPushButton(this);
    2. btn[j] = button;
    3. ...
    4. ...
    5. sendSignalMapper->setMapping(button, j);
    6. connect(button, SIGNAL(clicked()), sendSignalMapper, SLOT(map()), Qt::uniqueConnection);
    To copy to clipboard, switch view to plain text mode 

    Try to write your code with list of pointers instead of array.
    Don't use global variable make it member of class.

    CHEERS

  5. The following user says thank you to karankumar1609 for this useful post:

    rdkk (4th June 2013)

  6. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically created buttons to one slot

    QButtonGroup

    less methods to call, no need for an array just to address buttons per index later on.

    Cheers,
    _

  7. #5
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    2

    Default Re: Dynamically created buttons to one slot

    thanks for replies mates it solved itself and now works fine

  8. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically created buttons to one slot

    Quote Originally Posted by rdkk View Post
    thanks for replies mates it solved itself and now works fine
    I wish my code would do that. WHAT IS YOUR SECRET!?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamically created buttons to one slot

    Quote Originally Posted by amleto View Post
    I wish my code would do that. WHAT IS YOUR SECRET!?
    Maybe he wrote a program that modifies its own source code, rebuilds itself and re-executes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. use value from dynamically created widget in a slot
    By Cyrebo in forum Qt Programming
    Replies: 55
    Last Post: 23rd April 2013, 14:51
  2. Accessing Dynamically created Checkboxe
    By premroxx in forum Newbie
    Replies: 1
    Last Post: 6th November 2012, 07:14
  3. Replies: 12
    Last Post: 24th October 2011, 07:56
  4. Two dynamically created object interaction issue
    By kornicameister in forum Qt Quick
    Replies: 2
    Last Post: 9th September 2011, 11:35
  5. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 09:40

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
  •  
Qt is a trademark of The Qt Company.