I'm working with the Qt KDE Necessitas project. I have a project built in Qt Creator and I am installing the apk on an emulator API-15 (also tested on API-10).
The following code is setup to clear the text of two different QLineEdit objects when a button is clicked, but this isn't the case. Randomly, only one of the two QLineEdit objects are cleared.
mainwindow.h:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
public slots:
void slotClear();
private:
QLineEdit* line1;
QLineEdit* line2;
//...
};
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout* mainLayout = new QVBoxLayout;
QFormLayout* form = new QFormLayout;
line1 = new QLineEdit;
form->addRow(tr("Line 1: "), line1);
line2 = new QLineEdit;
form->addRow(tr("Line 2:"), line2);
QPushButton* button = new QPushButton;
mainLayout->addLayout(form);
mainLayout->addWidget(button);
QWidget* centralWid = new QWidget(this);
centralWid->setLayout(mainLayout);
this->setCentralWidget(centralWid);
connect(button, SIGNAL(clicked()), this, SLOT(slotClear()));
}
void MainWindow::slotClear()
{
line1->clear();
line2->clear();
}
//...
Calling the function QLineEdit::setText("") produces the same results. Additionally, connecting the clicked() signal from the button directly to the clear() slot of the QLineEdit has no effect.
I haven't been programming in Qt for very long, so I am unsure if there is something I am doing wrong. Is anybody seeing something needs to be corrected in order to have the text cleared from BOTH QLineEdits? I am not sure if this is unique to Qt itself or Qt Necessitas. Any input would be greatly appreciated.
EDIT
I have also just noticed that entering text in one line, switching to another line and entering text there, and then switching back to the original line results in the original text being erased once the field is clicked (note, the button was never clicked). I think this is a pretty clear indication that something funky is going on.
EDIT 2
Registered as a bug with KDE
Related
I want to be able to change page of QStackedWidget with some kind of animation (like fade in/out or others...)
after some research I find out maybe its possible with QGraphicsOpacityEffect, then I found this codes in here
Fade In Your Widget
// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
Fade Out Your Widget
// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(1);
a->setEndValue(0);
a->setEasingCurve(QEasingCurve::OutBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
connect(a,SIGNAL(finished()),this,SLOT(hideThisWidget()));
// now implement a slot called hideThisWidget() to do
// things like hide any background dimmer, etc.
but looks like these codes have some problem when used in QWidget inside of QStackedWidget i mean widget successfully fade in and out, but after animation finish if I minimize the windows the widget will disappear completely! (Im still able to see widget in bottom right corner of my window, looks like its pos changed?!)
btw my program is frameless.
thanks for help.
here is a example from my problem
test.cpp
Test::Test(QWidget *parent)
: CustomMainWindow(parent)
{
ui.setupUi(this);
setShadow(ui.bg_app);
connect(ui.close_app_btn, &QPushButton::clicked, this, &QWidget::close);
connect(ui.minimize_app_btn, &QPushButton::clicked, this, &QWidget::showMinimized);
QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
ui.checking->setGraphicsEffect(eff); // checking is my widget inside of QStackedWidget.
QPropertyAnimation* a = new QPropertyAnimation(eff, "opacity");
a->setDuration(350);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
}
CustomMainWindow.cpp
CustomMainWindow::CustomMainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowFlags(windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
setAttribute(Qt::WA_TranslucentBackground);
}
void CustomMainWindow::setShadow(QWidget* window)
{
QGraphicsDropShadowEffect* windowShadow = new QGraphicsDropShadowEffect;
windowShadow->setBlurRadius(9.0);
windowShadow->setColor(palette().color(QPalette::Highlight));
windowShadow->setOffset(0.0);
window->setGraphicsEffect(windowShadow);
}
when I run my program with this code, at first its successfully Fade In, but if I for example minimize the window the widget move from its original position to somewhere else, look at this gif
Note: MainWindow is the name of my class.
Header file:
//...
private slots:
void animationStackedWidgets();
void whenAnimationFinish();
//....
CPP file:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->button, &QPushButton::clicked, this, &MainWindow::animationStackedWidgets);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::animationStackedWidgets()
{
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
ui->stackedWidget->setGraphicsEffect(effect);
QPropertyAnimation *anim = new QPropertyAnimation(effectSw,"opacity");
anim->setDuration(350);
anim->setStartValue(0);
anim->setEndValue(1);
anim->setEasingCurve(QEasingCurve::InBack);
anim->start(QPropertyAnimation::DeleteWhenStopped);
connect(anim, SIGNAL(finished()), this, SLOT(whenAnimationFinish()));
}
void MainWindow::whenAnimationFinish()
{
ui->stackedWidget->setGraphicsEffect(0); // remove effect
}
I am writing a small utility composed by :
1) 4 QToolBar
2) 1 QToolBox
3) 1 QMenu
4) 1 QGraphicsView
I have some buttons on the QToolBox, each button represents a grid with different spacing.
Everytime a user clicks a button on the QToolBox the spacing on the QGraphicsScene` changes.
I use daily the new notation and never had any problems with it until I had to use an "abstract" entity such as the QAbstractButton approach.
the problem I have is that I don't seem to properly set the connection signal/slot of the QAbstractButton:
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void backgroundButtonClicked(QAbstractButton *button);
private:
void createToolBox();
QButtonGroup *buttonGroup;
QButtonGroup *backgroundButtonGroup;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createToolBox();
scene = new DiagramScene(itemMenu,this);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(toolBox);
view = new QGraphicsView(scene);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
}
void MainWindow::backgroundButtonClicked(QAbstractButton *button)
{
QList<QAbstractButton*> buttons = backgroundButtonGroup->buttons();
foreach (QAbstractButton *myButton, buttons) {
if(myButton != button)
button->setChecked(false);
}
QString text = button->text();
if(text == tr("Blue Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background1.png"));
else if(text == tr("White Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background2.png"));
else if(text == tr("Gray Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background3.png"));
else
scene->setBackgroundBrush(QPixmap(":/images/background4.png"));
scene->update();
view->update();
}
void MainWindow::createToolBox()
{
buttonGroup = new QButtonGroup(this);
buttonGroup->setExclusive(false);
QGridLayout *layout = new QGridLayout;
layout->addWidget(createCellWidget(tr("Conditional"), DiagramItem::Conditional), 0, 0);
layout->addWidget(createCellWidget(tr("Process"), DiagramItem::Step), 0, 1);
layout->addWidget(createCellWidget(tr("Input/Output"), DiagramItem::Io), 1, 0);
QToolButton *textButton = new QToolButton;
textButton->setCheckable(true);
buttonGroup->addButton(textButton, InsertTextButton);
textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png")));
textButton->setIconSize(QSize(50, 50));
QWidget *itemWidget = new QWidget;
itemWidget->setLayout(layout);
backgroundButtonGroup = new QButtonGroup(this);
// This is my first attempt but it does not work:
QObject::connect(backgroundButtonGroup, &QAbstractButton::clicked, this, &QAbstractButton::backgroundButtonClicked());
}
Different options I tried are:
1) via static_cast<> trying to cast it into the QAbstractButton as shown below:
QObject::connect(static_cast<QAbstractButton*>(backgroundButtonGroup), this, static_cast<QAbstractButton>(backgroundButtonClicked(void)));
2) Also I tried the following according to official documentation but that also didn't work:
QObject::connect(backgroundButtonGroup, &QAbstractButton::clicked, this, &QAbstractButton::backgroundButtonClicked);
Adding to that I was digressing towards the following structure below (heading to Q_INVOKE) but I am afraid I was over complicating the issue, but just wanted to include the additional solution I was trying to explore:
const bool connected = connect(sender, &Sender::aSignal,
receiver, &Receiver::aSlot);
Q_ASSERT(connected);
Q_UNUSED(connected);
Thanks for pointing to the right direction for solving this issue.
You have the following errors:
QButtonGroup does not have the clicked signal but buttonClicked.
You must not use ().
The syntax in general is: obj, & Class_of_obj, in your case backgroundButtonGroup is QButtonGroup and this is MainWindow so preliminarily the syntax is:
QObject::connect(backgroundButtonGroup, &QButtonGroup::buttonClicked, this, &MainWindow::backgroundButtonClicked);
But buttonClicked is overload then you must use QOverload to indicate the signature.
Considering the above, the solution is:
QObject::connect(backgroundButtonGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), this, &MainWindow::backgroundButtonClicked);
I recommend you check:
the QButtonGroup docs where it shows the example of how to make the connection.
In addition to New Signal Slot Syntax that indicates the various cases
In my project, I am using two QPushButton and two QLineEdit. I am connecting these QPushButton with these QLineEdit in such a way, so that QPushButton allow the user to select a folder from hard drive and after selection, the corresponding QLineEdit will display the URL path of the selected folder.
I also like to allow the user to write the URL by himself own if he does not want to click QPushButton and choose folder. And also if the user wants, he can also edit the URL after selecting by QPushButton.
Here I am facing two problems.
1) One QLineEdit allows user to write but another one does not.
2) When user presses on QPushButton, writing mode on corresponding QLineEdit becomes disabled.
The following is the code. Here InputLine and OutputLine are two QLineEdit
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* Setting the size of Mainwindow */
this->setWindowTitle("Crop Multiple Object");
this->setFixedHeight(600);
this->setFixedWidth(800);
/* Setting QLabel for displaying Image */
QLabel* image= new QLabel(this);
image->setGeometry(20,130,500,430);
image->setStyleSheet("QLabel {background-color: rgb(200,200,200)}");
image->show();
/* Set input URL */
QPushButton* InputURL = new QPushButton(this);
InputURL->setText("Input URL");
InputURL->setGeometry(20,30,100,30);
connect(InputURL, SIGNAL(clicked(bool)), this, SLOT(ReceiveInputURL()));
/* Set output URL */
QPushButton* OutputURL = new QPushButton(this);
OutputURL->setText("Output URL");
OutputURL->setGeometry(20,80,100,30);
connect(OutputURL, SIGNAL(clicked(bool)), this, SLOT(ReceiveOutputURL()));
/* Set Input URL Line*/
InputLine->setGeometry(140,30,400,30);
OutputLine->setGeometry(140,80,400,30);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ReceiveInputURL()
{
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
QString dir = QFileDialog::getExistingDirectory(this, tr("Input Image File"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if(!dir.isEmpty())
{
InputLine->setText(dir + "/");
}
}
void MainWindow::ReceiveOutputURL()
{
QFileDialog dialog(this);
dialog.setViewMode(QFileDialog::Detail);
QString dir = QFileDialog::getExistingDirectory(this, tr("Output Image File"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if(!dir.isEmpty())
{
OutputLine->setText(dir+ "/");
}
}
I appreciate any help. Thanks in advance.
The problem is that you are creating your QLineEdit objects before the centralWidget of MainWindow is created. This puts the central widget on top of your QLineEdit widgets, so it blocks the mouse events from passing through. To test this, you can disable mouse events for central widget with centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);, and you will notice that your QLineEdit widgets can be accessed by mouse clicks.
However, you shouldn't place any widgets directly on the MainWindow. This is not how QMainWindow is supposed to be used. Instead you should place your widgets on the centralWidget. You should read the docs of QMainWindow to know more.
I am trying to create a drop down button using QToolBar.
I tried to do that using the following strategy:
http://qt-project.org/forums/viewthread/5377
The problem is that the button doesn't respond immediately when I click on it. It takes several clicks to make the menu appear.
I guess I am putting the code in the wrong place, but where else should I put it?
Here is my code:
Proto::Proto(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Proto)
{
ui->setupUi(this);
QMenu *menu = new QMenu("Menu");
QWidgetAction *action = new QWidgetAction(this);
QPushButton *button2 = new QPushButton("Click me", menu);
action->setDefaultWidget(button2);
menu->addAction(action);
ui->btnVolume->setMenu(menu);
}
Please, anyone can help me?
Thanks in advance,
Seems like you forget to connect your button to a slot (or at least, it's not shown in the piece of code you posted). I just added the connect() statement like that
QPushButton *button2 = new QPushButton("Click me", menu);
connect(button2, SIGNAL(clicked()), SLOT(dosmth()));
action->setDefaultWidget(button2);
and then implemented this simple dosmth() Q_SLOT
void MainWindow::dosmth() {
qDebug() << "Hi";
}
and at each button click, i get this on the console:
Hi
Hi
Hi
I'm having extreme trouble getting QWidgets to expand as needed. This isn't the first time I've run into this problem, and last time I solved it by hacking in a large sizeHint(). This is the WRONG approach and I would really like to learn the CORRECT approach.
If anyone could help me out it would be greatly appreciated. Here's what it looks like and the layouts I have written in code. If necessary I can supply the code. Please help me learn Layouts.
Edit: The first layout mock up actually works correctly. I am attaching another layout mockup which causes a problem.
Code. Three classes.
MainWindow Class:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget * w = new MainCentralWidget(this);
setCentralWidget(w);
}
MainCentralWidget Class:
MainCentralWidget::MainCentralWidget(QWidget *parent) :
QWidget(parent)
{
SetupLayout();
}
void MainCentralWidget::SetupLayout()
{
QVBoxLayout * main_layout;
QFormLayout * plugin_layout;
//Start
main_layout = new QVBoxLayout();
//Setup the plugin chooser
plugin_layout = new QFormLayout();
QComboBox * plugins_box = new QComboBox();
plugin_layout->addRow("Choose Plugin: ", plugins_box);
QFrame* line = new QFrame();
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
plugin_layout->addRow(line);
main_layout->insertLayout(0, plugin_layout);
main_layout->insertWidget(1, new SubWidget());
//Finish
setLayout(main_layout);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
SubWidget Class:
SubWidget::SubWidget(QWidget *parent) :
QWidget(parent)
{
setStyleSheet("QWidget { background: yellow }");
setMaximumSize(10000,10000);
setMinimumSize(100,100);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
SetupLayout();
//setMaximumSize(10000,10000);
}
void SubWidget::SetupLayout()
{
QHBoxLayout main_layout;
main_layout.setAlignment(Qt::AlignTop | Qt::AlignLeft);
main_layout.addWidget(&m_graphics_view);
m_graphics_view.setMaximumSize(10000,100000);
setLayout(&main_layout);
}
void SubWidget::SetupLayout()
{
QHBoxLayout main_layout;
main_layout.setAlignment(Qt::AlignTop | Qt::AlignLeft);
main_layout.addWidget(&m_graphics_view);
m_graphics_view.setMaximumSize(10000,100000);
setLayout(&main_layout);
}
Your main_layout gets destroyed immediately after SetupLayout finished, as it goes out of scope. So, actually, your SubWidget has no any layout. That is why widgets are shown incorrectly.