Custom widget appears as a white rectangle when show is called



  • Hi, I have a newbie question that's driving my crazy for a few days.

    I designed a custom widget with other widgets inside, using a frame to set the items instead of a layout. So, in order to test it, I tried to create a new one of these widget whitout parent to show it in another window, but anything appears, only a window with the right size. I can't find what is the problem...

    Here is te code

    Custom widget header

    class FilterWindow : public QWidget
    {
    	Q_OBJECT
    
    public:
    	explicit FilterWindow(QWidget* parent = NULL);
    	FilterWindow(QWidget* parent, std::string);
    	~FilterWindow();
    
    	void setUi(QWidget* parent, std::string tittle);
    	void press();
    	void setData(std::string, std::vector<std::string> = std::vector<std::string>(), int rw = -1);
    	
    	//Overrided
    	QSize size() const;
    	QSize sizeHint() const;
    	void render(QPaintDevice * target, const QPoint & targetOffset = QPoint(), const QRegion & sourceRegion = QRegion(), RenderFlags renderFlags = RenderFlags(DrawWindowBackground | DrawChildren));
    	void copy(FilterWindow&);
    
    signals:
    	void btnprsd(int row, bool prsd);
    	void applyPrsd(std::vector<std::string>);
    
    public slots:
    	void pressedSlot();
    	void pressedApplySlot();
    
    private:
    	QFrame* Frame;
    	QCheckBox* checkBox;
    	QPushButton* ArrowBtn;
    	QTableWidget* Table;
    	QPushButton* ResetBtn;
    	QPushButton* AplyBtn;
    	std::vector<std::string> list;
    
    	int rw;
    	bool pressed;
    };
    

    Custom widget source

    
    FilterWindow::FilterWindow(QWidget* parent) : QObject(parent)
    {
    	this->setParent(parent);
    	setUi(parent, "");
    	pressed = false;
    	press();
    	
    	connect(ArrowBtn, SIGNAL(pressed()), this, SLOT(pressedSlot()));
    	connect(AplyBtn, SIGNAL(pressed()), this, SLOT(pressedApplySlot()));
    }
    
    FilterWindow::FilterWindow(QWidget* parent, std::string ttle)
    {
    	setUi(parent, ttle);
    	pressed = false;
    	press();
    
    	connect(ArrowBtn, SIGNAL(pressed()), this, SLOT(pressedSlot()));
    	connect(AplyBtn, SIGNAL(pressed()), this, SLOT(pressedApplySlot()));
    }
    
    void FilterWindow::copy(FilterWindow& inout)
    {
    	inout.setData(checkBox->text().toStdString(), list, rw);
    }
    
    FilterWindow::~FilterWindow()
    {
    	if (checkBox != NULL)
    		delete checkBox;
    	
    	if (ArrowBtn != NULL)
    		delete ArrowBtn;
    	
    	if (Table != NULL)
    		delete Table;
    
    	if (ResetBtn != NULL)
    		delete ResetBtn;
    	
    	if (AplyBtn != NULL)
    		delete AplyBtn;
    
    	if (Frame != NULL)
    		delete Frame;
    }
    
    void FilterWindow::setUi(QWidget* parent, std::string tittle)
    {
    	Frame = new QFrame(parent);
    	Frame->setObjectName(QStringLiteral("Frame"));
    	Frame->setFixedWidth(281);
    	Frame->setFixedHeight(89);
    	Frame->setStyleSheet(QLatin1String("QFrame\n"
    		"{\n"
    		"	border-style: solid;\n"
    		"	border-width: 0px;\n"
    		"	border-radius: 4px;\n"
    		"	border-color: transparent;\n"
    		"}"));
    	Frame->setFrameShape(QFrame::StyledPanel);
    	Frame->setFrameShadow(QFrame::Raised);
    	Frame->setAttribute(Qt::WA_DeleteOnClose);
    	checkBox = new QCheckBox(Frame);
    	checkBox->setText(tittle.c_str());
    	checkBox->setObjectName(QStringLiteral("checkBox"));
    	checkBox->setGeometry(QRect(0, 0, 248, 35));
    	checkBox->setAutoFillBackground(false);
    	checkBox->setStyleSheet(QLatin1String("QCheckBox\n"
    		"{\n"
    		"	background-color: rgb(128,139,143);\n"
    		"	color: white;\n"
    		"	border-radius: 4px;\n"
    		"	padding-left: 8px;\n"
    		"}\n"
    		"\n"
    		"QCheckBox::indicator:unchecked {\n"
    		"    image: url(:/OptiNanoPro/uchk);\n"
    		"}\n"
    		"\n"
    		"\n"
    		"QCheckBox::indicator:checked {\n"
    		"    image: url(:/OptiNanoPro/chk);\n"
    		"}\n"
    		""));
    	checkBox->setChecked(false);
    	ArrowBtn = new QPushButton(Frame);
    	ArrowBtn->setObjectName(QStringLiteral("ArrowBtn"));
    	ArrowBtn->setGeometry(QRect(246, 0, 36, 35));
    	ArrowBtn->setAutoFillBackground(true);
    	QIcon icon;
    	icon.addFile(QStringLiteral(":/OptiNanoPro/DownArrwLd"), QSize(), QIcon::Normal, QIcon::Off);
    	ArrowBtn->setIcon(icon);
    	ArrowBtn->setIconSize(QSize(36, 35));
    	ArrowBtn->setFlat(true);
    	Table = new QTableWidget(Frame);
    	Table->setObjectName(QStringLiteral("Table"));
    	Table->setGeometry(QRect(0, 35, 281, 31));
    	Table->setStyleSheet(QLatin1String("QTableWidget\n"
    		"{\n"
    		"	background-color: white;\n"
    		"	border-radius: 0px;\n"
    		"}"));
    	ResetBtn = new QPushButton(Frame);
    	ResetBtn->setText("Reset");
    	ResetBtn->setObjectName(QStringLiteral("ResetBtn"));
    	ResetBtn->setGeometry(QRect(0, 66, 169, 23));
    	ResetBtn->setStyleSheet(QLatin1String("QPushButton\n"
    		"{\n"
    		"	background-color: white;\n"
    		"	border-width: 1px;\n"
    		"    border-style: solid;\n"
    		"    border-color: rgb(86,86,84);\n"
    		"	border-radius: 3px;\n"
    		"	color: rgb(86,86,84);\n"
    		"}"));
    	AplyBtn = new QPushButton(Frame);
    	AplyBtn->setText("Apply");
    	AplyBtn->setObjectName(QStringLiteral("AplyBtn"));
    	AplyBtn->setGeometry(QRect(169, 66, 111, 23));
    	AplyBtn->setStyleSheet(QLatin1String("QPushButton\n"
    		"{\n"
    		"	background-color: white;\n"
    		"	border-width: 1px;\n"
    		"    border-style: solid;\n"
    		"    border-color: rgb(86,86,84);\n"
    		"	border-radius: 3px;\n"
    		"	color: rgb(86,86,84);\n"
    		"}"));
    }
    
    void FilterWindow::press()
    {
    	if (pressed)
    	{
    		Frame->setFixedHeight(89);
    		ArrowBtn->setIcon(QIcon(":/OptiNanoPro/UpArrwLd"));
    	}
    
    	else
    	{
    		Frame->setFixedHeight(35);
    		ArrowBtn->setIcon(QIcon(":/OptiNanoPro/DownArrwLd"));
    	}
    }
    
    void FilterWindow::setData(std::string name, std::vector<std::string> _list, int _rw)
    {
    	if (!name.empty())
    	{
    		checkBox->setText(name.c_str());
    	}
    
    	if (!_list.empty())
    	{
    		Table->clear();
    		Table->setColumnCount(2);
    		
    		for (int i = 0; i < _list.size(); i++)
    		{
    			QTableWidgetItem* itm = new QTableWidgetItem(_list.at(i).c_str());
    
    			Table->insertRow(i);
    			Table->setCellWidget(i, 0, new QCheckBox);
    			Table->setItem(i,1,itm);	
    		}
    
    		list = _list;
    	}
    
    	if (rw != -1)
    	{
    		rw = _rw;
    	}
    }
    
    QSize FilterWindow::size() const
    {
    	return Frame->size();
    }
    
    QSize FilterWindow::sizeHint() const
    {
    	return QSize(281, 89);
    }
    
    
    void FilterWindow::render(QPaintDevice * target, const QPoint & targetOffset, const QRegion & sourceRegion, RenderFlags renderFlags)
    {
    	Frame->render(target);
    }
    
    //Slots
    void FilterWindow::pressedSlot()
    {
    	pressed = !pressed;
    	press();
    
    	emit(btnprsd(rw,pressed));
    }
    
    void FilterWindow::pressedApplySlot()
    {
    	for (int i = 0; i < Table->rowCount(); i++)
    	{
    
    	}
    
    	emit(applyPrsd(list));
    }
    

    And this is what I make to call it

    FilterWindow* aux = new FilterWindow(); //No parent to force new window
    aux->show();

    Thanks


  • Lifetime Qt Champion

    Hi,

    Your handling of parent is wrong. You are using the parent of your FilterWindow object as your internal widgets parent which means they are not related to the FilterWindow instance.

    Please take a look at Qt's examples and tutorials to see how to properly create custom widgets.

    On a side note, your constructors also are not correct, the base class of FilterWindow is not QObject but QWidget, in the other one you're not calling the base class constructor.



  • @SGaist

    Thank you so much!

    It worked perfectly


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.