C++/Qt/ToolTip — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Версия 17:21, 25 мая 2010
Adding HTML based tooltip
<source lang="cpp"> Foundations of Qt Development\Chapter09\tooltips\main.cpp /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- include <QApplication>
- include "tooltipdialog.h"
int main( int argc, char **argv ) {
QApplication app( argc, argv ); ToolTipDialog dlg; dlg.show(); return app.exec();
}
Foundations of Qt Development\Chapter09\tooltips\tooltipdialog.cpp /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- include <QPushButton>
- include <QCheckBox>
- include <QLabel>
- include <QGroupBox>
- include <QGridLayout>
- include "tooltipdialog.h"
ToolTipDialog::ToolTipDialog() : QDialog() {
QGroupBox *groupBox = new QGroupBox( tr("Group") ); QGridLayout *gbLayout = new QGridLayout( groupBox ); QCheckBox *checkBox = new QCheckBox( tr("Check!") ); QLabel *label = new QLabel( tr("label") ); QPushButton *pushButton = new QPushButton( tr("Push me!") ); gbLayout->addWidget( checkBox, 0, 0 ); gbLayout->addWidget( label, 0, 1 ); gbLayout->addWidget( pushButton, 1, 0, 1, 2 ); QGridLayout *dlgLayout = new QGridLayout( this ); dlgLayout->addWidget( groupBox, 0, 0 ); checkBox->setToolTip( tr("This is a simple tool tip for the check box.") ); groupBox->setToolTip( tr("This is a group box tool tip.\n" "Notice that it appears between and around the contained widgets.\n" "It is also spanning several lines.") );label->setToolTip( tr("
It is possible to do lists.
" "- "
"
- You can format text. " "
- Bold is possible too. " "
- And the color and " "size. " "
You can do ordered lists as well.
" "- "
"
- First. " "
- Second. " "
- Third. " "
pushButton->setToolTip( tr("<img src=":/images/qt.png">" "You can also insert images into your tool tips.") );
}
Foundations of Qt Development\Chapter09\tooltips\tooltipdialog.h /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- ifndef TOOLTIPDIALOG_H
- define TOOLTIPDIALOG_H
- include <QDialog>
class ToolTipDialog : public QDialog { public:
ToolTipDialog();
};
- endif // TOOLTIPDIALOG_H
</source>
Adding image to tooltip
<source lang="cpp">
Foundations of Qt Development\Chapter09\tooltips\main.cpp /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- include <QApplication>
- include "tooltipdialog.h"
int main( int argc, char **argv ) {
QApplication app( argc, argv ); ToolTipDialog dlg; dlg.show(); return app.exec();
}
Foundations of Qt Development\Chapter09\tooltips\tooltipdialog.cpp /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- include <QPushButton>
- include <QCheckBox>
- include <QLabel>
- include <QGroupBox>
- include <QGridLayout>
- include "tooltipdialog.h"
ToolTipDialog::ToolTipDialog() : QDialog() {
QGroupBox *groupBox = new QGroupBox( tr("Group") ); QGridLayout *gbLayout = new QGridLayout( groupBox ); QCheckBox *checkBox = new QCheckBox( tr("Check!") ); QLabel *label = new QLabel( tr("label") ); QPushButton *pushButton = new QPushButton( tr("Push me!") ); gbLayout->addWidget( checkBox, 0, 0 ); gbLayout->addWidget( label, 0, 1 ); gbLayout->addWidget( pushButton, 1, 0, 1, 2 ); QGridLayout *dlgLayout = new QGridLayout( this ); dlgLayout->addWidget( groupBox, 0, 0 ); checkBox->setToolTip( tr("This is a simple tool tip for the check box.") ); groupBox->setToolTip( tr("This is a group box tool tip.\n" "Notice that it appears between and around the contained widgets.\n" "It is also spanning several lines.") );label->setToolTip( tr("
It is possible to do lists.
" "- "
"
- You can format text. " "
- Bold is possible too. " "
- And the color and " "size. " "
You can do ordered lists as well.
" "- "
"
- First. " "
- Second. " "
- Third. " "
pushButton->setToolTip( tr("<img src=":/images/qt.png">" "You can also insert images into your tool tips.") );
}
Foundations of Qt Development\Chapter09\tooltips\tooltipdialog.h /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- ifndef TOOLTIPDIALOG_H
- define TOOLTIPDIALOG_H
- include <QDialog>
class ToolTipDialog : public QDialog { public:
ToolTipDialog();
};
- endif // TOOLTIPDIALOG_H
</source>
Set zone for tooltip
<source lang="cpp"> Foundations of Qt Development\Chapter09\tooltipzones\main.cpp /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- include <QApplication>
- include "tipzones.h"
int main( int argc, char **argv ) {
QApplication app( argc, argv ); TipZones tipZones; tipZones.show(); return app.exec();
}
Foundations of Qt Development\Chapter09\tooltipzones\tipzones.cpp /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- include <QPainter>
- include <QPaintEvent>
- include <QEvent>
- include <QHelpEvent>
- include "tipzones.h"
TipZones::TipZones() : QWidget() { } void TipZones::paintEvent( QPaintEvent* ) {
QRect redRect, greenRect, blueRect, yellowRect; redRect = QRect( 0, 0, width()/2, height()/2 ); greenRect = QRect( width()/2, 0, width()/2, height()/2 ); blueRect = QRect( 0, height()/2, width()/2, height()/2 ); yellowRect = QRect( width()/2, height()/2, width()/2, height()/2 ); QPainter p( this ); p.setPen( Qt::black ); p.setBrush( Qt::red ); p.drawRect( redRect ); p.setBrush( Qt::green ); p.drawRect( greenRect ); p.setBrush( Qt::blue ); p.drawRect( blueRect ); p.setBrush( Qt::yellow ); p.drawRect( yellowRect );
}
bool TipZones::event( QEvent *event ) {
if( event->type() == QEvent::ToolTip ) { QHelpEvent *helpEvent = static_cast<QHelpEvent*>( event ); QRect redRect, greenRect, blueRect, yellowRect; redRect = QRect( 0, 0, width()/2, height()/2 ); greenRect = QRect( width()/2, 0, width()/2, height()/2 ); blueRect = QRect( 0, height()/2, width()/2, height()/2 ); yellowRect = QRect( width()/2, height()/2, width()/2, height()/2 ); if( redRect.contains( helpEvent->pos() ) ) setToolTip( tr("Red") ); else if( greenRect.contains( helpEvent->pos() ) ) setToolTip( tr("Green") ); else if( blueRect.contains( helpEvent->pos() ) ) setToolTip( tr("Blue") ); else setToolTip( tr("Yellow") ); } return QWidget::event( event );
}
Foundations of Qt Development\Chapter09\tooltipzones\tipzones.h /*
* Copyright (c) 2006-2007, Johan Thelin * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of APress nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */
- ifndef TIPZONES_H
- define TIPZONES_H
- include <QWidget>
class TipZones : public QWidget { public:
TipZones();
protected:
void paintEvent( QPaintEvent* ); bool event( QEvent* );
};
- endif // TIPZONES_H
</source>