PDA

View Full Version : creat own figure/checkbox



ReasyEasyPeasy
12th July 2015, 21:06
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

anda_skoa
13th July 2015, 00:28
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,
_

ReasyEasyPeasy
14th July 2015, 10:35
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:

#ifndef SYMBOLBUTTON_H
#define SYMBOLBUTTON_H
#include <QWidget>
#include <QPushButton>
#include <QString>
class SymbolButton : public QPushButton
{
Q_OBJECT
public:
int counter;
SymbolButton(const QString& text, QWidget* parent = NULL);
void changetext();
};

#endif // SYMBOLBUTTON_H


#include "symbolbutton.h"
#include <QMessageBox>
#include <QWidget>
#include <QPushButton>
SymbolButton::SymbolButton(const QString& text, QWidget* parent) : QPushButton(text,parent)
{
connect(this,SIGNAL(clicked()),this,changetext());

}
void SymbolButton::changetext(){
this->counter++;
if ( this->counter>5)
this->counter=0;
setText("test");
}


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QPushButton>
#include "symbolbutton.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
SymbolButton *button = new SymbolButton;
ui->setupUi(this);



int rows = ui->gridLayout->rowCount();
int cols = ui->gridLayout->columnCount();

ui->gridLayout->addWidget(button,rows,cols,0);
}

MainWindow::~MainWindow()
{
delete ui;
}
This button should just change its text if u click on it and set counter = counter + 1.

anda_skoa
14th July 2015, 11:14
You forgot to initialize "counter"

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

Cheers,
_

ReasyEasyPeasy
14th July 2015, 11:30
hm i dont get it. :O
Where and how i have to initialize counter?

SymbolButton::SymbolButton(const QString& text, QWidget* parent) : QPushButton(text,parent)
{
int counter = 0;// like this?
connect(this,SIGNAL(clicked()),this,changetext());

}
void SymbolButton::changetext(){
this->counter++;
if (counter>5)
counter=0;
setText("test");
}

anda_skoa
14th July 2015, 13:49
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

ReasyEasyPeasy
14th July 2015, 14:03
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


#include "symbolbutton.h"
#include <QMessageBox>
#include <QWidget>
#include <QPushButton>

SymbolButton::SymbolButton(const QString& text, QWidget* parent) : QPushButton(text,parent)
{
this->counter = 0;
connect(this,SIGNAL(clicked()),this,changetext());

}
void SymbolButton::changetext(){
this->counter++;
if (this->counter>5)
this->counter=0;
setText("test");
}


EDIT:
ok i made a slot out of the funciton "changetext" and now i only get the second error:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QPushButton>
#include "symbolbutton.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
SymbolButton *button = new SymbolButton;
ui->setupUi(this);



int rows = ui->gridLayout->rowCount();
int cols = ui->gridLayout->columnCount();

ui->gridLayout->addWidget(button,rows,cols,0);
}

MainWindow::~MainWindow()
{
delete ui;
}

if i write

QPushButton *button = new QPushButton;
instead of


SymbolButton *button = new SymbolButton;
in mainwindow.cpp why?

anda_skoa
14th July 2015, 14:21
Well like this?

yes


Error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const'

SLOT() macro missing around the slot argument.



Error: C2512: 'SymbolButton': No appropriate default constructor available

Your constructor requires a QString argument, you are trying to call it without.

Cheers,
_

ReasyEasyPeasy
14th July 2015, 14:23
ITS WORKING!!! thx :D ^^
:-*

d_stranz
14th July 2015, 15:46
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:


void SymbolButton::changetext(){
this->counter++;
if (this->counter>5)
this->counter=0;
setText( QString( "test%1" ).arg( counter ) );
}