3

Google マップのズームイン、ズームアウトに似たボタンを作成しようとしています。アイコンと同じ大きさのボタンが欲しい:

http://codegeekz.com/wp-content/uploads/google-maps-jquery.jpg

(画像を掲載せず申し訳ありません。評判が足りないようです)。

QAction を使用しようとしていますが、何らかの理由でボタンが表示されません。QAction を使用して別のプロジェクトでボタンを作成しましたが、関連するすべてのコードをコピーしてもうまくいきませんでした (単にインスタンスが表示されません)。これらは必需品です:

メインウィンドウ.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QAction>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);

protected slots:
    void addEntry();

private:
    QAction *addButton;
};
#endif // MAINWINDOW_H

メインウィンドウ.cpp

#include "mainwindow.h"
#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget *window = new QWidget;
    QVBoxLayout *container = new QVBoxLayout();

    //Horizontal add/subtract layout------------------------------------

    QHBoxLayout *layer1 = new QHBoxLayout();

    QAction *addButton = new QAction((QIcon("/home/kyle/Desktop/add1.png")),"Add Entry", this);

    addAction(addButton);

    connect(addButton, SIGNAL(triggered()), this, SLOT(addEntry()));

    //Scroll Layout------------------------------------

    QHBoxLayout *layer2 = new QHBoxLayout();
    ...

    container->addLayout(layer1);
    container->addLayout(layer2);

    window->setLayout(container);
    setCentralWidget(window);
}

void
MainWindow::addEntry(){
    ...
}

私は持っている:

  • 空のウィジェットで addAction() を試し、それをレイアウトに追加しました。
  • .h ファイルで QAction オブジェクトを宣言 (QPainter を使用する際の一般的な問題)
  • QPushButton を使用してみました (非常に醜いですが動作します)。

何が間違っているか、または他のボタンのようなオブジェクトの推奨事項についてのアイデアをいただければ幸いです。質問もお気軽にどうぞ。最終的に作成したいボタンは、QHBoxLayout で操作できる小さなウィジェットです。

4

1 に答える 1

7
  1. を呼び出すときaddAction(addButton);、どこにアクションを追加するつもりですか。例:ui->mainToolBar->addAction(addButton);

  2. QPushButtonあなたの要件を満たします。スタイルシートを使用してプッシュボタンのスタイルを設定できます。

例:

    QPushButton *addButton = new QPushButton(QIcon(":/plus.png"),"");
    QString buttonStyle = "QPushButton{border:none;background-color:rgba(255, 255, 255,100);}";
    addButton->setStyleSheet(buttonStyle); // Style sheet
    addButton->setIconSize(QSize(50,50));
    addButton->setMinimumSize(50,50);
    addButton->setMaximumSize(50,50);
    layer1->addWidget(addButton);// The horizontal layout
  1. ここでQtのスタイルシートの例を参照できます
于 2015-07-23T07:46:15.777 に答える