Invisible Buttons
-
A layout is needed to layout the buttons :-) Otherwise they are painted more or less randomly, may overlap or another widget may be painted on top of them and the like. Also, your buttons have neither text nor an icon.
Time to read "Layout Management":http://doc.qt.nokia.com/4.7/layout.html in the docs. You will run into serious problems if you do not layout your UI elements (either manually or using Qt's layout classes).
-
thanks for your answers!
but i don't think that the problem is another object in front of the buttons, because i can click these buttons. a label changes when i click one of these buttons. so i think that there is no paintevent for these buttons, but why? everything else is working fine, and in simulator the buttons are shown. so they are there, but just not painted...i can't test at the moment, but i will try it with a layout.
-
[quote author="Matze5590" date="1299849226"]thanks for your answers!
but i don't think that the problem is another object in front of the buttons, because i can click these buttons. a label changes when i click one of these buttons. so i think that there is no paintevent for these buttons, but why? everything else is working fine, and in simulator the buttons are shown. so they are there, but just not painted...i can't test at the moment, but i will try it with a layout. [/quote]
It can be a label in front, disabling the paint, as it paints itself on top. As a label dioes not react on a mouse click, it is send to the next widget, which could be the button...
-
ok, i made a small example of my problem:
main.cpp:
@#include <QtGui/QApplication>
#include "mainwindow.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;w.showFullScreen(); return a.exec();
}
@mainwindow.h:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QPushButton>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
QPushButton *btn[2][2];private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
bool eventFilter(QObject *obj, QEvent *ev);
};#endif // MAINWINDOW_H
@mainwindow.cpp:
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
for (int i = 0; i < 2; i ++){
for (int k = 0; k < 2; k ++){
btn[i][k] = new QPushButton("", ui->widget);
btn[i][k]->setGeometry(i * 75 + 80, k * 75 + 200, 75, 75);
btn[i][k]->installEventFilter(this);
btn[i][k]->show();
}
}
ui->pushButton_2->setEnabled(true);
ui->pushButton->setEnabled(false);
}void MainWindow::on_pushButton_2_clicked()
{
for (int i = 0; i < 2; i ++){
for (int k = 0; k < 2; k ++){
btn[i][k]->~QPushButton();
}
}
ui->pushButton_2->setEnabled(false);
ui->pushButton->setEnabled(true);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *ev)
{
for (int i = 0; i < 2; i ++){
for (int k = 0; k < 2; k ++){
if (obj == btn[i][k] && ev->type() == QEvent::MouseButtonRelease){
QString str;
str.sprintf("%ld, %ld", i, k);
ui->label->setText(str);
}
}
}
}
@in simulator i can see the buttons, but not on Nokia N8. But i can click them!
-
If you give the buttons a text, is it visible?
Are the buttons and text visible if you put them into a layout?
Some further hints:
- You must not call the destructor method yourself (line 34)! You must call
@
delete btn[i][k];
@- You should avoid QString::sprintf (line 46) and replace it with arg:
@
QString str = QString("%1, %2").arg(i).arg(k);
@- Your event filter method lacks a return value.