Results 1 to 10 of 10

Thread: creat own figure/checkbox

  1. #1
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default creat own figure/checkbox

    Hey guys,
    first of all english isnt my first language. So im sorry for my rough english.
    First problem:
    Im new to qt and i need a little advice how i can find a start for my problem.
    I need to creat an own figure. Its extend of triangles and rectangles. This figure should have some areas i can fill with information. Like the text from a pushbutton. The figure should be able to move around with the mouse and i want to link them with a line. Which qt classes/fuctions do i need for this?

    second problem:
    i need a box like a checkbox but this box needs to count the clicks and 4 different symbols instead of just the check.if the user clicks once show symbol one and so on.
    I dont need a complete solution just the point where i have to start.
    i use the qt editor.
    greets ReasyEasyPeasy

  2. #2
    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: creat own figure/checkbox

    For the first problem I suggest to have a look at QGraphicsView.

    For the second problem, maybe a QLabel for which you reimplement mousePressEvent() and mouseReleaseEvent().
    Or maybe a QPushButton subclass with a counter.

    Cheers,
    _

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

    ReasyEasyPeasy (14th July 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: creat own figure/checkbox

    hey,
    thanks for this fast answer.
    I tried to creat an own QPushButton but it doesnt work. Maybe u can help me?
    I tryed the following:
    Qt Code:
    1. #ifndef SYMBOLBUTTON_H
    2. #define SYMBOLBUTTON_H
    3. #include <QWidget>
    4. #include <QPushButton>
    5. #include <QString>
    6. class SymbolButton : public QPushButton
    7. {
    8. Q_OBJECT
    9. public:
    10. int counter;
    11. SymbolButton(const QString& text, QWidget* parent = NULL);
    12. void changetext();
    13. };
    14.  
    15. #endif // SYMBOLBUTTON_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "symbolbutton.h"
    2. #include <QMessageBox>
    3. #include <QWidget>
    4. #include <QPushButton>
    5. SymbolButton::SymbolButton(const QString& text, QWidget* parent) : QPushButton(text,parent)
    6. {
    7. connect(this,SIGNAL(clicked()),this,changetext());
    8.  
    9. }
    10. void SymbolButton::changetext(){
    11. this->counter++;
    12. if ( this->counter>5)
    13. this->counter=0;
    14. setText("test");
    15. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QWidget>
    4. #include <QPushButton>
    5. #include "symbolbutton.h"
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. SymbolButton *button = new SymbolButton;
    11. ui->setupUi(this);
    12.  
    13.  
    14.  
    15. int rows = ui->gridLayout->rowCount();
    16. int cols = ui->gridLayout->columnCount();
    17.  
    18. ui->gridLayout->addWidget(button,rows,cols,0);
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 
    This button should just change its text if u click on it and set counter = counter + 1.
    Last edited by anda_skoa; 14th July 2015 at 11:12. Reason: changed [quote] to [code]

  5. #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: creat own figure/checkbox

    You forgot to initialize "counter"

    And you are always setting the same text, independent of the value of "counter".

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    ReasyEasyPeasy (14th July 2015)

  7. #5
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: creat own figure/checkbox

    hm i dont get it. :O
    Where and how i have to initialize counter?
    Qt Code:
    1. SymbolButton::SymbolButton(const QString& text, QWidget* parent) : QPushButton(text,parent)
    2. {
    3. int counter = 0;// like this?
    4. connect(this,SIGNAL(clicked()),this,changetext());
    5.  
    6. }
    7. void SymbolButton::changetext(){
    8. this->counter++;
    9. if (counter>5)
    10. counter=0;
    11. setText("test");
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 14th July 2015 at 13:48. Reason: changed [quote] to [code]

  8. #6
    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: creat own figure/checkbox

    In the constructor, yes, but not like that.
    That defines a new, local, variable called "counter" and initializes it.

    Cheers,
    _

    P.S.: use code tags for code, not quote tags

  9. The following user says thank you to anda_skoa for this useful post:

    ReasyEasyPeasy (14th July 2015)

  10. #7
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: creat own figure/checkbox

    Well like this? Its still not working.
    i get 2 errors.
    Error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const'
    Error: C2512: 'SymbolButton': No appropriate default constructor available
    Qt Code:
    1. #include "symbolbutton.h"
    2. #include <QMessageBox>
    3. #include <QWidget>
    4. #include <QPushButton>
    5.  
    6. SymbolButton::SymbolButton(const QString& text, QWidget* parent) : QPushButton(text,parent)
    7. {
    8. this->counter = 0;
    9. connect(this,SIGNAL(clicked()),this,changetext());
    10.  
    11. }
    12. void SymbolButton::changetext(){
    13. this->counter++;
    14. if (this->counter>5)
    15. this->counter=0;
    16. setText("test");
    17. }
    To copy to clipboard, switch view to plain text mode 
    EDIT:
    ok i made a slot out of the funciton "changetext" and now i only get the second error:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QWidget>
    4. #include <QPushButton>
    5. #include "symbolbutton.h"
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. SymbolButton *button = new SymbolButton;
    11. ui->setupUi(this);
    12.  
    13.  
    14.  
    15. int rows = ui->gridLayout->rowCount();
    16. int cols = ui->gridLayout->columnCount();
    17.  
    18. ui->gridLayout->addWidget(button,rows,cols,0);
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 
    if i write
    QPushButton *button = new QPushButton;
    instead of
    Qt Code:
    1. SymbolButton *button = new SymbolButton;
    To copy to clipboard, switch view to plain text mode 
    in mainwindow.cpp why?
    Last edited by ReasyEasyPeasy; 14th July 2015 at 14:22.

  11. #8
    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: creat own figure/checkbox

    Quote Originally Posted by ReasyEasyPeasy View Post
    Well like this?
    yes

    Quote Originally Posted by ReasyEasyPeasy View Post
    Error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const'
    SLOT() macro missing around the slot argument.

    Quote Originally Posted by ReasyEasyPeasy View Post
    Error: C2512: 'SymbolButton': No appropriate default constructor available
    Your constructor requires a QString argument, you are trying to call it without.

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    ReasyEasyPeasy (14th July 2015)

  13. #9
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: creat own figure/checkbox

    ITS WORKING!!! thx ^^
    :-*

  14. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: creat own figure/checkbox

    ITS WORKING!!!
    if (counter>5)
    counter=0;
    No, it isn't. In your original post, you said you wanted to count 4 clicks. This code counts 6 clicks. Try this and you'll see:

    Qt Code:
    1. void SymbolButton::changetext(){
    2. this->counter++;
    3. if (this->counter>5)
    4. this->counter=0;
    5. setText( QString( "test%1" ).arg( counter ) );
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 1st June 2011, 14:54
  2. Qt Creat or crashes when QPrinter object is declared
    By fredykhan in forum Qt Programming
    Replies: 13
    Last Post: 18th February 2011, 18:44
  3. Draw figure underneath other figure
    By Mnemonic in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 13:38
  4. How to creat different shape of pushbutton?
    By Manohar in forum Qt Tools
    Replies: 16
    Last Post: 28th March 2008, 14:16
  5. qmake creat Makefile.Debug has error
    By freegnu in forum Qt Programming
    Replies: 4
    Last Post: 12th June 2006, 07:31

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.