I am brand new to Qt development and am trying to make a simple "Hello, World" with a toggle-button. I have something like this:
with the corresponding code:
void MainWindow::on_pushButton_toggled(bool checked)
{
if(checked) {
ui->label->setText("Hello, World!");
} else {
ui->label->setText("");
}
}
My goal is to change the label's text either from blank to "Hello, World!" or vice versa on a button click.
Currently the label only stays blank. Earlier the default text was "TextLabel", and on a button-click, it would change from "TextLabel" to blank, which is almost correct.
I've tried different options like on_pushButton_clicked, but to no avail.
I know the solution is a simple one -- can anyone point me in a good direction?
Why don't you do it like this?
void MainWindow::on_pushButton_toggled(bool checked)
{
ui->label->setText(ui->label->text().isEmpty() ? "Hello, World!" : "");
}
You need to declare your callback method as below :
class MainWindow : public QMainWindow
{
Q_OBJECT
bool checked;
QPushButton *my_button;
private slots:
void handleButton();
};
Then in your Constructor you need to connect Signal and Slot as below :
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
checked = 0;
connect(my_button, SIGNAL (released()), this, SLOT (on_pushButton_toggled()));
}
Your call back method will be like this. You can toggle your flag in this method as every time you click this SLOT will be called.
void MainWindow::on_pushButton_toggled()
{
checked = ~checked;
if(checked) {
ui->label->setText("Hello, World!");
} else {
ui->label->setText("");
}
}
I hope this will work out for you.
QPushButton is not checkable by default; you need to call QAbstractButton::setCheckable(true); before you can really use the QAbstractButton::toggled signal in any meaningful way. What happens in your code is that your slot is being called with the value false passed every single time because the button simply isn't checkable.
Related
I am designing a Qt application using Qt Creator. As a part of the application, I need to be able to get position, height and width information of QRubberBand before I hide() it. For that purpose I tried to use the following logic, which is given in the documentation of QRubberBand:
void Widget::mousePressEvent(QMouseEvent *event)
{
origin = event->pos();
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
rubberBand->hide();
// determine selection, for example using QRect::intersects()
// and QRect::contains().
}
And defined rubberBand as below in the private section of the header file:
QRubberBand *rubberBand;
After doing that it works well. To go to the next step I defined the following integers as well (private section of the header):
int rubX;
int rubY;
int rubWidth;
int rubHeight;
And I tried to get geometry information before hiding rubberBand in mouseReleaseEvent similar to the following:
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
rubX = rubberBand->x();
rubY = rubberBand->y();
rubHeight = rubberBand->height();
rubWidth = rubberBand->width();
rubberBand->hide();
}
When I added those codes, the program runs, but when I try drawing the rubberBand, the program crashes giving SIGSEGV.
So here are my questions:
Why this is happening?
Is it possible to accomplish my goal by slightly editing the code?
What should I do to get what I want?
I know that I have done a foolish mistake, but I have not found it yet. Do not hesitate to comment if you want to get more information about the question.
From the code and comments you appear to be assuming that the rubberBand member will automatically be initialized to nullptr. However, it's actually uninitialized making the following...
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
result in undefined behaviour.
Change your Widget constructor to include an explicit initialization...
Widget::Widget ()
: rubberBand(nullptr)
{
...
}
Better still, just make rubberBand a non-pointer member...
QRubberBand rubberBand;
and initialize it in the constructor with...
Widget::Widget ()
: rubberBand(QRubberBand::Rectangle, this)
{
...
}
I have like 10 buttons on my UI and I gotta check which one was touched. I was using the following logic and it was working fine, but now I am getting this error for some reason:
NullReferenceException: Object reference not set to an instance of an object
DetectButton.Start () (at Assets/Scripts/DetectButton.cs:14)
Any ideas what could be going on? Here is my code (attached to the canvas), and I am using Unity version 5.1.0f3. If you need any other info I will gladly provide, thanks in advance
void Start()
{
this.GetComponent<Button>().onClick.AddListener(() =>
{
if (this.name == "btnJogadores2")
{
print ("2 jogadores");
jogadores = 2;
}
//QuantidadeJogadores(this.name);
//QuantidadePartidas(this.name);
});
}
You don't have to all this the way you are doing.
An Easier and good practice would be to create 10 separate GameObjects for each button inside your canvas. and then create a single script with 10 separate functions for all those buttons in it. Attach that script to you canvas. and then on the button GameObject select the script on the desired function. Sample below
void Start() { }
void Update() { }
public void button1()
{
Debug.Log("Button3");
}
public void button2()
{
Debug.Log("Button1");
}
public void button3()
{
Debug.Log("Button3");
}
NOTE: button1, button2 and button3 are the functions for 3 separate buttons
Then inside your unity Inspector:
Select your script with you button functions.
Assign you desired method to you button.
After this run your scene and your button will call the assigned methods properly.
Code is not tested, but it should get you started to get all the Buttons.
void Start() {
var buttons = this.GetComponents<Button> ();
foreach(var button in buttons) {
button.onClick.AddListener(() = > {
if (this.name == "btnJogadores2") {
print("2 jogadores");
jogadores = 2;
}
//QuantidadeJogadores(this.name);
//QuantidadePartidas(this.name);
});
}
}
Actually it will be hard to distinguish between the buttons.
The more practical aproach would be to make 10 GameObjects (Child of the Canvas) and attach your Script to everyone of them.
Is there any way to check that the ui elements(line edit,combo box,etc.) of the dialog has been changed.
What I want is to show a message to the user if he changes the value of any single ui element, saying that details have been partially filled.
What i can do is use connect for each ui element & based on the value changed of each element i am setting a boolean flag & at the time of close event i am checking that boolean flag.
But Its quite complicate to check it for each widget.
Is there any easier way.
Code that I am using for single ui element is,
connect(ui->leAge,SIGNAL(textChanged(QString)),this,SLOT(functChanged())); //In Constructor
void DemoDialog::functChanged() //Will be called if value of line edit (ui->leAge) is changed
{
flag=true;
}
void DemoDialog::closeEvent(QCloseEvent *event)
{
if (flag) {
if (QMessageBox::warning(this,"Close","Do you want to close?",QMessageBox::Yes|QMessageBox::No)==QMessageBox::Yes) {
this->close();
}
}
You can't reimplement closeEvent to prevent closing a window. The close() call that you do is either redundant or an error (infinite recursion), since a closeEvent method call is just a way of being notified that a closing is imminent. At that point it's too late to do anything about it.
Keep in mind the following:
Closing a dialog usually is equivalent to canceling the dialog. Only clicking OK should accept the changes.
When a user wants to close a dialog, you don't have to ask them about it. They initiated the action. But:
It is proper to ask a user about dialog closure if there are changes have not been accepted - on platforms other than OS X.
So, you have to do several things:
Reimplement the void event(QEvent*) method. This allows you to reject the close event.
Offer Apply/Reset/Cancel buttons.
Your flag approach can be automated. You can find all the controls of the dialog box and set the connections automatically. Repeat the statement below for every type of control - this gets tedious rather quickly:
foreach(QTextEdit* w, findChildren<QTextEdit*>())
connect(w, SIGNAL(textChanged(QString)), SLOT(functChanged()));
You can leverage the meta property system. Most controls have a user property - that's the property that holds the primary value of the control (like text, selected item, etc). You can scan all of the widget children, and connect the property change notification signal of the user property to your flag:
QMetaMethod slot = metaObject().method(
metaObject().indexOfSlot("functChanged()"));
foreach (QWidget* w, findChildren<QWidget*>()) {
QMetaObject mo = w->metaObject();
if (!mo.userProperty().isValid() || !mo.userProperty().hasNotifySignal())
continue;
connect(w, mo.notifySignal(), this, slot);
}
Each widget is a QObject. QObjects can have properties, and one of the properties can be declared to be the user property. Most editable widget controls have such a property, and it denotes the user input (text, numerical value, selected index of the item, etc.). Usually such properties also have change notification signals. So all you do is get the QMetaMethod denoting the notification signal, and connect it to your function that sets the flag.
To determine the changed fields, you don't necessarily need a flag. In many dialog boxes, it makes sense to have a data structure that represent the data in the dialog. You can then have a get and set method that retrieves the data from the dialog, or sets it on the dialog. To check for changed data, simply compare the original data to current data:
struct UserData {
QString name;
int age;
UserData(const QString & name_, int age_) :
name(name_), age(age_) {}
UserData() {}
};
class DialogBase : public QDialog {
QDialogButtonBox m_box;
protected:
QDialogButtonBox & buttonBox() { return m_box; }
virtual void isAccepted() {}
virtual void isApplied() {}
virtual void isReset() {}
virtual void isRejected() {}
public:
DialogBase(QWidget * parent = 0) : QDialog(parent) {
m_box.addButton(QDialogButtonBox::Apply);
m_box.addButton(QDialogButtonBox::Reset);
m_box.addButton(QDialogButtonBox::Cancel);
m_box.addButton(QDialogButtonBox::Ok);
connect(&m_box, SIGNAL(accepted()), SLOT(accept()));
connect(&m_box, SIGNAL(rejected()), SLOT(reject()));
connect(this, &QDialog::accepted, []{ isAccepted(); });
connect(this, &QDialog::rejected, []{ isRejected(); });
connect(&buttonBox(), &QDialogButtonBox::clicked, [this](QAbstractButton* btn){
if (m_box.buttonRole(btn) == QDialogButtonBox::ApplyRole)
isApplied();
else if (m_box.buttonRole(btn) == QDialogButtonBox::ResetRole)
isReset();
});
}
}
class UserDialog : public DialogBase {
QFormLayout m_layout;
QLineEdit m_name;
QSpinBox m_age;
UserData m_initialData;
public:
UserDialog(QWidget * parent = 0) : QDialog(parent), m_layout(this) {
m_layout.addRow("Name", &m_name);
m_layout.addRow("Age", &m_age);
m_age.setRange(0, 200);
m_layout.addRow(&buttonBox());
}
/// Used by external objects to be notified that the settings
/// have changed and should be immediately put in effect.
/// This signal is emitted when the data was changed.
Q_SIGNAL void applied(UserData const &);
UserData get() const {
return UserData(
m_name.text(), m_age.value());
}
void set(const UserData & data) {
m_name.setText(data.name);
m_age.setValue(data.age);
}
void setInitial(const UserData & data) { m_initialData = data; }
bool isModified() const { return get() == m_initialData; }
protected:
void isAccepted() Q_DECL_OVERRIDE { emit applied(get()); }
void isApplied() Q_DECL_OVERRIDE { emit applied(get()); }
void isReset() Q_DECL_OVERRIDE { set(m_initialData); }
};
If you're only checking whether the input fields are filled when the Dialog closes, you don't need the flags you can only check if there is any input.
If you are filling the input fields programatically at some points but are also only interested in the change when the dialog closes, you can also check in the close function whether the current input is equal to the one you set earlier.
From the code you posted, I can't really see what you need the flags for.
How to make a QT dialog read-only? Any general way to implement it easily? For example
(1) set all its containing widgets disable. (how to implement it?)
(2) Intercept edit events like key pressed, mouse pressed but how not to intercept the one to close the dialog?
I think this feature should be very helpful.
Disabling the widgets can be done similar to the following:
void myDialog::disableWidgets()
{
QList<QWidget *> widgets = this->findChildren<QWidget *>();
foreach(QWidget* widget, widgets)
{
widget->setEnabled(false);
}
}
To intercept events, QDialog includes the function installEventFilter(QObject*).
This allows you to use a separate object to receive all events passed to the dialog. You can then choose to handle the event in the object, or pass it on to the dialog itself by calling the base class QObject::eventFilter
class MyEventHandler : public QObject
{
Q_OBJECT
protected:
bool MyEventHandler::eventFilter(QObject *obj, QEvent *event)
{
// handle key press events
if (event->type() == QEvent::KeyPress)
{
// Do something
// ...
return true; // event handled by the class
}
else
{ // ignore this event and pass it to the dialog as usual
return QObject::eventFilter(obj, event);
}
}
return false;
};
QDialog* dlg = new QDialog;
MyEventHandler evtHandler = new MyEventHandler;
dlg->installEventFilter(evtHandler);
Read-only is a strange term to apply to a dialog. Disabling all widgets as above does the trick. If you only wanted to make the input part of a QInputDialog read-only (while leaving scrollbars, buttons, etc. enabled), you could adapt that code as below:
QInputDialog dialog(this);
dialog.setOptions(QInputDialog::UsePlainTextEditForTextInput);
dialog.setWindowTitle("Title");
dialog.setLabelText("Label");
dialog.setTextValue("1\n2\n3\n");
QList<QWidget *> widgets = dialog.findChildren<QWidget *>();
foreach(QWidget* widget, widgets) {
if (strcmp(widget->metaObject()->className(),"QPlainTextEdit")==0) {
QPlainTextEdit *t = static_cast<QPlainTextEdit*>(widget);
t->setReadOnly(true);
}
}
dialog.exec();
I'm writing a qt-based c++ application. I have a number of buttons that I want to be mutually exclusive - only one can be toggled at a time. I generally use a QButtonGroup for this - it provides a nice logical way to manage sets of buttons. When one gets pressed, the previously-pressed one gets unpressed, which is exactly the behavior I want.
This time, however, I'd like to allow for the group to be entirely unchecked. Unfortunately this seems to be disallowed by QButtonGroup:
exclusive : bool
This property holds whether the button group is exclusive.
If this property is true then only one button in the group can be
checked at any given time. The user can click on any button to check
it, and that button will replace the existing one as the checked
button in the group.
In an exclusive group, the user cannot uncheck the currently checked
button by clicking on it; instead, another button in the group must be
clicked to set the new checked button for that group.
There are a number of ways to work around this, of course. I'm wondering if there's a pre-made alternative to QButtonGroup that allows this behavior, so that 1) I'm not reinventing the wheel and 2) I can stay within idiomatic qt to make project management easier in the future.
Any suggestions?
In Qt5, I use a similar solution as Laurent Michel's, but using the release event instead of the press event:
// Allow to uncheck button in exclusive group
void CustomButton::mouseReleaseEvent(QMouseEvent* a_Event) {
if(group()->checkedId()==group()->id(this)) {
if(isDown()) group()->setExclusive(false);
}
QToolButton::mouseReleaseEvent(a_Event);
group()->setExclusive(true);
}
For the sake of completeness, I would like to publish here one possible solution to the problem, as I just solved it in my case. Just beware that the following code is valid for Qt3. It may as well work for Qt4 and Qt5, because it doesn't use a lot of stuff.
So, I assume that I have a widget CustomWidget somewhere that contains buttons (of type CustomButton) and that one and only one button can be switched on. If one clicks another button in the widget, then the currently switched on button is switched off and the newly clicked button is switched on.
The CustomButtons contained in the CustomWidget are all contained in a QButtonGroup in the following way:
QButtonGroup* m_ButtonGroup = new QButtonGroup(this);
m_ButtonGroup->hide();
m_ButtonGroup->insert(Btn1);
m_ButtonGroup->insert(Btn2);
m_ButtonGroup->insert(Btn3);
m_ButtonGroup->setExclusive(true);
Here, Btn1, Btn2, and Btn3 are of type CustomButton
class CustomButton : public QToolButton
{
Q_OBJECT
public:
CustomButton (QWidget* apo_parent = 0, const char* as_name = 0);
virtual ~CustomButton ();
protected:
virtual void mousePressEvent(QMouseEvent* a_Event);
};
The method you want to implement specially is mousePressEvent. If its body is implemented in the following way:
void CustomButton ::mousePressEvent(QMouseEvent* a_Event)
{
if(group() && isToggleButton())
{
CustomButton* selectedButton(dynamic_cast<CustomButton*>(group()->selected()));
if(selectedButton)
{
if(selectedButton->name() == name())
{
group()->setExclusive(false);
toggle();
group()->setExclusive(true);
return;
}
}
}
QToolButton::mousePressEvent(a_Event);
}
then the widget behaves as you want.
Another similar solution as the previous answers, but using nextCheckState() which appears to be the more natural extension point to me:
void MyButton::setSemiExclusive(bool value)
{
mSemiExclusive = value;
}
void MyButton::nextCheckState()
{
if (mSemiExclusive)
{
if (auto g = group())
{
auto old = g->exclusive();
if (g->checkedButton() != this)
g->setExclusive(true);
QAbstractButton::nextCheckState();
g->setExclusive(old);
return;
}
}
QAbstractButton::nextCheckState();
}
This depends on the associated QButtonGroup having set exclusive to false.
If you don't want to extend the button class, you can also accomplish this by using signals (Qt5, Python):
from PySide import QtGui
class View(QtGui.QWidget):
def __init__(self):
self.buttonGroup = QtGui.QButtonGroup(self)
for button in buttons:
self.buttonGroup.addButton(button)
button.pressed.connect(buttonPressed)
button.released.connect(buttonReleased)
def buttonPressed(self):
button = self.sender()
checkedButton = self.buttonGroup.checkedButton()
if checkedButton != None and checkedButton.objectName() == button.objectName():
self.buttonGroup.setExclusive(False)
def buttonReleased(self):
button = self.sender()
if self.buttonGroup.exclusive() == False:
button.setChecked(False)
self.buttonGroup.setExclusive(True)
def manualDeselection:
self.buttonGroup.setExclusive(False)
self.buttonGroup.checkedButton().setChecked(False)
self.buttonGroup.setExclusive(True)
My solution is to derive the QButtonGroup, set it to non exclusive internaly and manage the states by yourself
class myQButtonGroup : public QButtonGroup
{
Q_OBJECT
public:
explicit myQButtonGroup(QObject *parent = Q_NULLPTR) : QButtonGroup(parent) {
_bExclusive = true;
QButtonGroup::setExclusive(false);
connect(this, SIGNAL(buttonClicked(QAbstractButton *)), SLOT(buttonClicked(QAbstractButton *)));
}
void setExclusive(bool bExclusive) { _bExclusive = bExclusive; }
bool exclusive() const { return _bExclusive; }
protected slots:
void buttonClicked(QAbstractButton *button) {
if (_bExclusive) {
// just uncheck all other buttons regardless of the state of the clicked button
QList<QAbstractButton *> buttonlist = buttons();
for (auto iBtn = buttonlist.begin(); iBtn != buttonlist.end(); ++iBtn) {
QAbstractButton *pBtn = *iBtn;
if (pBtn && pBtn != button && pBtn->isCheckable()) pBtn->setChecked(false);
}
}
}
protected:
bool _bExclusive;
};