Page 1 of 2 12 LastLast
Results 1 to 20 of 32

Thread: Qcombobox

  1. #1

    Default Qcombobox

    Hi

    I have QCombox without items. After clicking on the combobox I would like to showmessage but I cannot find slot "Clicked" or something similar.

    How to do it ?

    Regards
    Artur

  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: Qcombobox

    There is no such signal. If you want to have one, subclass, reimplement mouse events and emit such signal yourself.
    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. #3
    Join Date
    May 2015
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qcombobox

    ... in short, add a pushButton, eish, that is just mean - lol.

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qcombobox

    No. Do as Wysota has said. The mouse event will hook clicking the combo box and emit a "clicked" signal - perhaps only sometimes (when the combo box is empty). If the combo box isn't empty, you get a signal from the clicked item.

  5. #5

    Default Re: Qcombobox

    If it is not problem please give me an example.

    Thank you for help

  6. #6
    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: Qcombobox

    Quote Originally Posted by arturs View Post
    If it is not problem please give me an example.
    An example of what?
    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.


  7. #7

    Default Re: Qcombobox

    How to do it using QMouseEvent.
    How to determine that the left button of mouse was clicked on the Combobox.

  8. #8
    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: Qcombobox

    Quote Originally Posted by arturs View Post
    How to do it using QMouseEvent.
    How to determine that the left button of mouse was clicked on the Combobox.
    Qt Code:
    1. void Combo::mousePressEvent(QMouseEvent *e) {
    2. if(e->button() == Qt::LeftMouseButton) emit clicked();
    3. }
    To copy to clipboard, switch view to plain text mode 
    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.


  9. #9

    Default Re: Qcombobox

    My Code
    Qt Code:
    1. void Test::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. if (event->button()==Qt::LeftButton) emit clicked();
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    Should I create clicked signal ? because Now I have the following error: 'clicked' was not declared in this scope

  10. #10
    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: Qcombobox

    Quote Originally Posted by arturs View Post
    Should I create clicked signal ?
    If you want to emit a signal, the signal has to exist. So yes, you need to declare the signal in your class.
    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.


  11. #11

    Default Re: Qcombobox

    but It is still not clear for me. The signal will be sent each time when the left button of mouse is clicked even if the pointer of mouse there is out of Combobox area.

    Second question:
    Is it possible to check using a method if a slot was called by signal or was called another method

    for example:


    I have a slot

    Qt Code:
    1. void Test:: test1 (void) {
    2.  
    3. qDebug () << "TEST";
    4. }
    To copy to clipboard, switch view to plain text mode 

    I can call it using the following code:
    test();

    or it can be called by signal.

    I would like to know which method was used to call the slot.

  12. #12
    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: Qcombobox

    Quote Originally Posted by arturs View Post
    but It is still not clear for me. The signal will be sent each time when the left button of mouse is clicked even if the pointer of mouse there is out of Combobox area.
    No. A widget receives mouse events only when they happen over its area.

    Is it possible to check using a method if a slot was called by signal or was called another method
    QObject::sender()
    I would like to know which method was used to call the slot.
    What for?
    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.


  13. #13

    Default Re: Qcombobox

    As I thought It did not work something is wrong

    My code:


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMouseEvent>
    4. #include <QDebug>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. QObject::connect(ui->comboBox,SIGNAL(clicked()),this,SLOT(test()));
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19.  
    20. void MainWindow::mouseMoveEvent(QMouseEvent *event)
    21. {
    22. if (event->button()==Qt::LeftButton) emit clicked();
    23.  
    24. }
    25.  
    26.  
    27. void MainWindow::test (void){
    28.  
    29. qDebug () << "TEST SIGNAL";
    30.  
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17. void mouseMoveEvent(QMouseEvent *);
    18. void test (void);
    19. signals:
    20. void clicked(void);
    21.  
    22. private:
    23. Ui::MainWindow *ui;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 






    Quote Originally Posted by wysota View Post
    No. A widget receives mouse events only when they happen over its area.


    QObject::sender()

    What for?

    I have a method open_file and in the first case I choose proper file from C: disk (QfileDialog) in second case the same file is downloaded from server and write for example on C: disk.
    In second case I know file path so I do not know to choose file once again but used path which was chosen before writing file.

    How to skip the part of code which open Qfiledialog.

    Regards
    Artur

  14. #14
    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: Qcombobox

    Quote Originally Posted by arturs View Post
    Qt Code:
    1. void MainWindow::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. if (event->button()==Qt::LeftButton) emit clicked();
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 
    First of all this is a handler for mouseMoveEvent for which button() always returns 0 (obviously a move is not caused by a button). Second of all this is an event handler for MainWindow and not the combobox.

    I have a method open_file and in the first case I choose proper file from C: disk (QfileDialog) in second case the same file is downloaded from server and write for example on C: disk.
    In second case I know file path so I do not know to choose file once again but used path which was chosen before writing file.

    How to skip the part of code which open Qfiledialog.
    Why don't you store the path in some variable and if the variable is not empty you will know you already have the path?
    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.


  15. #15

    Default Re: Qcombobox

    I made a mistake. I do not know Why I used Mosemove...



    Qt Code:
    1. void QComboBox::mousePressEvent(QMouseEvent *event)
    2. {
    3. if (event->button()==Qt::LeftButton) emit clicked();
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    but know I have the error: 'clicked' was not declared in this scope

    something is wrog I am a bit confused

  16. #16
    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: Qcombobox

    You cannot define a handler for a class which already exists. You need to subclass QComboBox and use an instance of your subclass instead of QComboBox. Alternatively you can apply an event filter on the existing combobox if you want to avoid subclassing however this will make your code messy.
    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.


  17. #17

    Default Re: Qcombobox

    I created the following code:

    Qt Code:
    1. #include "mycombobox.h"
    2.  
    3. MyCombobox::MyCombobox(QWidget *parent) : QComboBox(parent)
    4. {
    5.  
    6. }
    7.  
    8. MyCombobox::~MyCombobox()
    9. {
    10.  
    11. }
    12.  
    13. void MyCombobox::mousePressEvent(QMouseEvent *event)
    14. {
    15.  
    16. if (event->button()==Qt::LeftButton) emit clicked();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef MYCOMBOBOX_H
    2. #define MYCOMBOBOX_H
    3.  
    4. #include <QObject>
    5. #include <QWidget>
    6. #include <QComboBox>
    7. #include <QMouseEvent>
    8.  
    9. class MyCombobox : public QComboBox
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MyCombobox(QWidget *parent =0 );
    15. ~MyCombobox();
    16.  
    17. protected:
    18.  
    19. void mousePressEvent(QMouseEvent *);
    20.  
    21. signals:
    22.  
    23. void clicked(void);
    24.  
    25. };
    26.  
    27. #endif // MYCOMBOBOX_H
    To copy to clipboard, switch view to plain text mode 


    but something is wrong I have the following errors: undefined reference to `vtable for MyCombobox' and I do not know what is wrong

    Thank you for your patience
    Last edited by arturs; 29th May 2015 at 17:39.

  18. #18
    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: Qcombobox

    Quote Originally Posted by arturs View Post
    Do you mean the solution ?
    I don't understand what you mean. Does it work?
    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.


  19. #19

    Default Re: Qcombobox

    I edited my previous post because I sent it by my mistake

  20. #20
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qcombobox

    I do not know what is wrong, either. IMO, the code should work. Try:
    (1) Remove QObject and QWidget includes - they are included by QComboBox
    (2) Add "virtual" specifiers to the dtor and the mouse handler. Well, you need not according to the C++ specification - because both methods are already virtual.
    (3) Remove the "void" parameter from clicked(). Well, the "old" syntax is still legal but contemporary C++ is rather "clicked()" than "clicked(void)"
    (3) If it does not help, delete the compiled files directory and make Creator to build everything from (real) scratch.

Similar Threads

  1. Replies: 1
    Last Post: 20th May 2015, 11:21
  2. QComboBox
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2014, 14:14
  3. QComboBox
    By kavinsiva in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2009, 16:11
  4. QComboBox
    By bismitapadhy in forum Qt Programming
    Replies: 7
    Last Post: 15th July 2009, 06:01
  5. QComboBox
    By coderbob in forum Qt Programming
    Replies: 4
    Last Post: 12th December 2007, 06:38

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.