[Interest] Masked widget glitch

Sensei senseiwa at gmail.com
Tue Jan 14 16:21:57 CET 2014


Dear all,

I am trying to make a popup dialog that appears with an arrow, and I'm 
doing this by applying a mask to a QWidget.

When the popup is displayed the first time, the border still is visible 
(MacOS X 10.9.1, Qt 4.8.5), and displaying the widget again makes the 
glitch disappear. See the attachment for the glitch.

If I show, hide, and re-show the widget obviously works, but I think my 
code is somehow broken.

Can you guys suggest me how to make it work better?

Thanks & Cheers!


===main.cpp===

#include <QtGui>

#include "simple.h"

int main(int argc, char *argv[])
{
     QApplication app(argc, argv);

     SimpleList w;
     w.show();
     return app.exec();
}

===simple.h===

#include <QtGui>

class SimpleList : public QListWidget
{
Q_OBJECT

public:

     SimpleList();

public slots:

     void editStep(QListWidgetItem *item);

     void editAccepted();

     void editRejected();

private:

     QWidget     *editor_;
     QVBoxLayout *editorLayout_;
     QLineEdit   *edit_;
     QDialogButtonBox *okcancel_;

     const int pad_    = 10;
     const int width_  = 350;
};


===simple.cpp===

#include "simple.h"

SimpleList::SimpleList() : QListWidget()
{
     // Add some items
     addItem("First");
     addItem("Second");
     addItem("Third");
     addItem("Fourth");
     addItem("Fifth");
	
     int w = 300;

     // The popup window
     editor_ = new QWidget(this);
     editor_->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
     editor_->setWindowModality(Qt::ApplicationModal);

     // Vertical layout
     editorLayout_ = new QVBoxLayout(editor_);

     // Set margins for a correct masked display
     int l, t, r, b;
     editorLayout_->getContentsMargins(&l, &t,       &r, &b);
     editorLayout_->setContentsMargins( l,  t + pad_,  r, b);

     // Create widgets
     edit_     = new QLineEdit(editor_);
     okcancel_ = new QDialogButtonBox(QDialogButtonBox::Ok | 
QDialogButtonBox::Cancel, Qt::Horizontal, editor_);
     editorLayout_->addWidget(edit_);
     editorLayout_->addWidget(okcancel_);

     editor_->setLayout(editorLayout_);

     // Set size
     editor_->resize(w, editor_->height());

     // Set the new policy: width cannot be changed
     editor_->setFixedWidth(width_);

     // Connect signals
     connect(this, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, 
SLOT(editStep(QListWidgetItem*)));
     connect(okcancel_, SIGNAL(accepted()), this, SLOT(editAccepted()));
     connect(okcancel_, SIGNAL(rejected()), this, SLOT(editRejected()));
};

void SimpleList::editStep(QListWidgetItem *item)
{
     QRect r = visualItemRect(currentItem());

     QPoint local(r.bottomLeft());
     QPoint global = mapToGlobal(local);

     global.setX(global.x() + r.width() / 2);
     global.setY(global.y());

     qWarning("edit (%d %d - %d %d)", local.x(), local.y(), global.x(), 
global.y());

     editor_->move(global);

     editor_->show();

     edit_->setText(item->text());
     edit_->setCursorPosition(0);

     // Height
     int h = editor_->geometry().height();

     // Set size
     editor_->resize(width_, h);

     // Set the mask to make it cool
     QPolygon mask;
     mask << QPoint(     0,  pad_);
     mask << QPoint(    10,  pad_);
     mask << QPoint(    20,     0);
     mask << QPoint(    30,  pad_);
     mask << QPoint(width_,  pad_);
     mask << QPoint(width_,     h);
     mask << QPoint(     0,     h);
     mask << QPoint(     0,  pad_);

     editor_->setMask(QRegion(mask));

     //editor_->hide();

     //editor_->show();
}


void SimpleList::editAccepted()
{
     qWarning("accepting change");
     currentItem()->setText(edit_->text());
     editor_->close();
}

void SimpleList::editRejected()
{
     qWarning("rejecting change");
     editor_->close();
}









-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2014-01-14 at 4.10.16pm.png
Type: image/png
Size: 55308 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140114/2528e5bb/attachment.png>


More information about the Interest mailing list